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

Jim McQuarrie
Jim McQuarrie
10,597 Points

confused here

While I passed this, I am confused as to why we would need the Math.random function when we are passing the parameters to the argument

max(1, 2);
alert(max); 

instead of max(1, 2); alert(Math.random(max));

The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

script.js

1 Answer

Samuel Webb
Samuel Webb
25,370 Points

Math.random() has nothing to do with the challenge. It was just an example to show you that it's possible to pass a function as an argument to another function. The goal was to get you to end up with this code:

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

alert(max(1,2)); //The example was a hint for you to do this line.

What will happen is first max() will run which will return an integer(the higher number), then it will alert() that number to the screen.

Jim McQuarrie
Jim McQuarrie
10,597 Points

yep, I realized that pretty quick :) thank you for your response