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

antonio morales
antonio morales
912 Points

i am stuck

i am not sure what i am doing wrong here

script.js
function max (bad,good) {
  if(bad > good) {
    return bad; 
  }
    return good;
    }
 max(bad,good);
if ( bad <good) {

}
alert( ("bad < good") );

3 Answers

Steven Parker
Steven Parker
231,275 Points

You need to move the call to the "max" function to inside the argument area (parentheses) of the "alert" call. And where you call the function, give it a couple of numbers for arguments instead of the parameter names.

And you won't need an "if" outside of the function.

antonio morales
antonio morales
912 Points

thank you
Steven Parker i always miss these things

Answer to first part:

function max(a,b) {

  if (a > b ) {

    return a;
    } else {

      return b;
      }
  }

Answer to second part:

function max(a,b) {

  if (a > b ) {

    return a;
    } else {

      return b;
      }
  }

alert( max(2,3) ) ;

Where you made the mistake:

1) you have not used the else keyword for the second condition

2) you need to remove the secobd if statement and instead replace with an else statement like in my example.

I hope that helped. All the best ?

Steven Parker
Steven Parker
231,275 Points

The function is correct as-is, no "else" is needed. That's because when the condition is true, the function exits — so effectively the entire remaining code in the function is only done "else".

So just having the first if statement would do?

Steven Parker
Steven Parker
231,275 Points

Yes, you only need the "if" and no "else". You never need an "else" when the "if" contains a return.

Thanks for the clarification ?