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

Call the function and display results of function in an alert.

Why do I get an error? alert (max (num1, num2));

3 Answers

Neil Docherty
Neil Docherty
10,418 Points

For part one of the challenge you want to create the function and give it two parameters. I've called my two parameters num1 and num2.

function max( num1, num2 ){
  if( num1 > num2 ){
    return num1;
  }
  if( num2 > num1 ){
    return num2;
  }
};

For the second part in this challenge al we want to do is call the function and put the result into an alert() dialogue.

We could do this in two ways.

  1. By creating a variable called result and assigning it the return value of the max() function. And then passing this result variable into the alert() function. This will open an alert box with the message 12.
var result = max( 12, 5 );
alert( result );
  1. The easier way to do this is to pass the max() function directly into the alert() function, like so.
alert( max( 12, 5 ) );

Hope this helps.

Sean T. Unwin
Sean T. Unwin
28,690 Points

Nothing is wrong with your code, per se. However, the Challenge wants you to pass acutal Numbers, as opposed to variables.

thank you for your help