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

console runs my code fine

While i ran the code in console, it works fine. But not here, why?

script.js
function max(value1, value2) {
    if (value1 > value2) {
        return value1;
    } else {
        return value2;
    }
}
max(15,25);
alert(Math.random));

2 Answers

You need to alert your function if you want to see it.

alert(max(15,25));

What's the difference between the code you wrote and i wrote?

The difference is you just called the function max, which will return the value, but then get rid of it cause your not doing anything with it, you're just calling it. You need to either save the result of calling your function so you can do something with it later, or just use the function call inside alert which will display the result immedietly then get rid of it.

So really, your code is running, it just doesn't seem like it is because you aren't displaying anything.

Does that make sense?

But where does Math.Random goes?

Well in your code you alert Math.random, which probably won't do what you would expect because Math.random is a function and if you wanted to see the result you would have to call it using parenthesis like so Math.random() otherwise, without parenthesis, you are just passing the function to alert, not the returned value of the function

Tommy Gebru
Tommy Gebru
30,164 Points

Hey Ammar, your original code has an extra parentheses or ")" and it called the Math.random function with no argument. Therefore it should not display anything. It has no value in this Code Challenge. However if you place the function max in alert, then max would be displayed because it passes the argument** 15 and 25 instead of placeholders **value1 and value2.

Hope this helps ;)

Or you could save your result and alert it

var maxResult = max(15,25);
alert(maxResult);