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

not sure what I need to change...

I'm not sure if my alert is in the right spot, it keeps saying "doesnt look like you used the alert method". What do I need to fix?

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

3 Answers

Steven Parker
Steven Parker
231,236 Points

The function shouldn't be modified. The alert call needs to be done outside (after) the function.

Thanks Steven, I went back and reviewed more videos and have tried it a bunch of different ways but now when I place the alert call outside the the function it says "task 1 is no longer passing" ...?

Adam Beer
Adam Beer
11,314 Points

When you finished the first task don't change the code in the second task. Use alert dialog and add inside to max(number1, number2) function. Change write number1,number2 to two numbers inside the max function.

Adam Beer
Adam Beer
11,314 Points

And please fixed the curly brackets inside the max function.

Adam Beer
Adam Beer
11,314 Points

When you simply use if statement without else if/else you don't need curly brackets..

Steven Parker
Steven Parker
231,236 Points

It doesn't matter if you have "else if" or "else", the braces ("curly brackets") are optional as long as the conditional body contains only one statement. So that part of this code is fine as-is.

My 2 cents as an intermediate, I would do this:

function max(number1, number2) {
  if (number1 > number2) {
    return number1;
}
  else {
return number2;
}

}

In other words, you failed to use curly brackets {} twice within the if statement

Steven Parker
Steven Parker
231,236 Points

As I mentioned above, the braces are optional when the conditional code is one single statement. So your example is fine, but so it the original code (in that regard).