Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Create a max() Function

function is not returning number

Challenge Task 1 of 2

Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would like). The function should return the larger of the two numbers.

HINT: You'll need to use a conditional statement to test the 2 parameters to see which is the larger of the two.

It always showing me that max function is not returning any number. What is wrong?

script.js
function max (big , small) {
  if (parseInt(big) > parseInt(small))
    return big;
}
AFIR MOURAD TAHAR
AFIR MOURAD TAHAR
3,766 Points

Hello,

your function indeed returns big. the problem is that you are not displaying it on the screen. add echo big ;

Or if I understood the question well. you should call the function.

function max (big , small) {

if (parseInt(big) > parseInt(small)) return big;

}

// add the following//

$result = max (2,1);

echo $result;

in this case, you will get 2 in your browser.

Thank you every one specially Ashley Skilton for help. Now I have clear idea what i was doing wrong. Thank you again.

5 Answers

Codin - Codesmite
Codin - Codesmite
8,600 Points

Your syntax for your if statement is slightly wrong, and you are only returning "big" you need to return your "small" variable if "small" is larger then "big".

function max (big, small) {
  if (big > small) {
    return big;
  }
  else {
    return small;
  }
}
Stanley Thijssen
Stanley Thijssen
22,831 Points

now you return small when the numbers are equal too :)

Samuel Webb
Samuel Webb
25,370 Points

Who cares which variable is returned if they're equal? It's the same number.

Samuel Webb
Samuel Webb
25,370 Points

Also, you're saying

if (max > small)

should be

if (big > small)
return big; //correct

return max; // incorrect
Codin - Codesmite
Codin - Codesmite
8,600 Points

Stanley if you do it the way you are saying:

function max (big, small) {
  if (big > small) {
    return big;
  }
  else if (small > big) {
    return small;
  }
}

If both numbers are equal, no value will be returned and as Samuel pointed out, it doesn't matter which variable is returned if they are equal, because it will return the same number which would be still the largest number.

Sorry fixed my typos in my code (just woke up, brain not functioning 100% yet lol)

Stanley Thijssen
Stanley Thijssen
22,831 Points

The challenge asks to return the bigger number. So if the numbers are equal there is no bigger number? So if you return small when they are equal small is not bigger but equal to big?

Samuel Webb
Samuel Webb
25,370 Points

Yep you're correct. Technically nothing should be returned if the numbers are equal. Seems to work either way though so... v0v

Samuel Webb
Samuel Webb
25,370 Points

The problem with your code is that an else statement is required, which means you'll have to use curly braces for your if statement. As of now, you're not returning the correct value if small is larger than big. You also don't need parseInt() for this challenge. You can just assumed that the user will be using numbers. Here's how your code should look:

function max (big , small) {
  if (big > small) {
    return big;
  } else {
    return small;
  }
}

If you really want to get fancy, use this:

function max(num1, num2) {
  var num;
  num1 < num2 ? num = num2 : num = num1; 
  return num;
}

The only reason I included this second example is so you can see another way to do an if/else statement. Just something interesting for you to check out when you have some free time.

Stanley Thijssen
Stanley Thijssen
22,831 Points

You have to add an else if with small > big and return small. The Challenge asked you to return the bigger number so there need to be 2 conditionals.

function max (big , small) {
    if (parseInt(big) > parseInt(small)) {
        return big;
    } else if ( parseInt(small) > parseInt(big) ) {
        return small;
    }
}
AFIR MOURAD TAHAR
AFIR MOURAD TAHAR
3,766 Points

Hello, your function indeed returns big. the problem is that you are not displaying it on the screen. add echo big ; Or if I understood the question well. you should call the function. function max (big , small) {

if (parseInt(big) > parseInt(small))

return big;

}

// add the following//

$result = max (2,1);

echo $result;

in this case, you will get 2 in your browser.

Janet Demel
Janet Demel
2,989 Points
function max(num1, num2){
  if( parseInt(num1) > parseInt(num2) ){
    return (num1);
  } else{
    return (num2);
  }  
}
max(3,4);