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

Khaleel Yusuf
Khaleel Yusuf
15,208 Points

How do I do this?

Beneath the max function you just created, call it with two numbers and display the results in an alert dialog. Pass the result of the function to the alert method.

For example, to display the results of the Math.random() method in an alert dialog you could type this:

alert( Math.random( ) ); How do I answer it

script.js
function max(n1, n2) {
if (n1 > n2) {
return n1;
} else {
return n2;
}
}
alert( Math.random(n1, n2 ) );

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You made a function named max and you're supposed to call it and give it two numbers. But instead, you're calling a Math.random function and sending in two undefined variables. Remember, n1 and n2 are undefined outside of the function. To call a function we simply give the name and then any arguments in parentheses. So the line you're missing is:

alert(max(100, 150));

This will call the max function and send in 100 which will be assigned to n1 and 150 which will be assigned to n2. Because the else statement will be executed, the value of n2 will be returned to the alert. This means that 150 will be printed out in the alert that pops up. Note that you could substitute the 100 and the 150 for any numbers of your choosing.

Hope this helps! :sparkles:

Vladut Astalos
Vladut Astalos
11,246 Points

That 'Math.random()' was just an example, in your alert method you need to call the function you just created with two numbers as argument. Those numbers can be any numbers you want. Your alert method should look something like this:

alert(max(4, 12));