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

Javascript Argument

function max (a,b ){ if (a>b){ return a ; }else{ return false; } } max (20, 10);

What is wrong in this code.? Any suggestion It should take two number as argument and return the greater number . Thanks in advance

script.js
function max (a,b ){
if (a>b){
return a ;
}else{
return false;
}
}
 max (20, 10);
Mathew Tran
Mathew Tran
Courses Plus Student 10,205 Points

For this function, it's probably good to sit down and think why you are having this error.

Q: "What is the max function supposed to do?" A: "It should return the greater number, given number A, and number B"

In layman terms, the max function should do the following:

Given 2 numbers, A and B: If A is bigger than B, return A If B is bigger or equal than A, return B

Your current code logic:

function max (a,b ){
    if (a>b){
        return a ;
    }else{
        return false;
    }
}

Your "else" statement is the issue :)

1 Answer

andren
andren
28,558 Points

Your code is quite close, but what happens if you say run your function like this: max(10, 50)? The function should return the largest number, but it will actually return false since the first argument is not greater than the second.

In your else code block you should return the second argument like this:

function max (a,b ){
  if (a>b){
    return a;
  } else{
    return b;
  }
}

Then your code will pass the first task.

appretiate that... moving forward .thanks