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

Why is what I have incorrect?

script.js
function max(num1, num2) {
  if (num1 > num2) {
    return num1; 
  }
  else{
    return num2;
  }
}
alert(max(num1, num2));

3 Answers

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

I think the reason it doesn't work is because you never say what num1 and num2 are, you might write the alert like this:

alert(max(1, 3));
Brian Holland
Brian Holland
3,508 Points

From the perspective of inside your function, num1 and num2 are defined. In your script as a whole, however, num1 and num2 are what's called undefined. In other words, the computer has never heard of them before and has no idea what they are.

Your code is fine, but just remember:

Function parameters are the names listed in the function definition:

function max(num1, num2){

//code stuff here

}

Function arguments are the real values passed to (and received by) the function: Look at Caleb's code:

alert(max(1,3));

I hope this helps, just remember to pass an argument to the function when you want to call the function.