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

It says it can't find "variable numberA" and I don't know what it means.

I don't even have a variable, let alone one called numberA, so I'm not sure what I'm doing wrong.

script.js
function max( numberA, numberB) { 
  if ( numberA >= numberB) {
    return numberA;
  } else if (numberA <= numberB)
    return numberB;
}
max(numberA, numberB);
alert(Math.random(numberA, numberB));

2 Answers

Heidi Puk Hermann
Heidi Puk Hermann
33,366 Points

You define two variables in your function that you choose to call numberA and numberB. When you call your function, you need to provide the actual values, not their names. An example could be:

max(1,2);

In this case numberA is set to 1 and numberB is set to 2. The max-function would return 2.

Heidi Puk Hermann
Heidi Puk Hermann
33,366 Points

ohh... and you do not have to call the Math.random() inside the alert(). It was just used as an example in the exercise. In stead your should call your max() inside the alert.

alert(max(num1,num2));

where num1 and num2 are two numbers that you choose.

  • You're trying to pass the numberA variable to the function, but it does not exist in your program
  • You're supposed to pass the function call to alert. They only used Math.random() in the instructions as a way to show you that a func call can be passed to alert