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

Sam DeClerk
Sam DeClerk
3,444 Points

For some reason code wont work in treehouse but does fine in console

I can't figure out exactly why my code isn't running, i'll try refreshing my browser. It worked fine in google's console.

script.js
function max(n1,n2) {
   if(n1 > n2){
    alert("the first number is larger than the second");
     return n1;
  }else if(n1 < n2){
    alert("The second number is bigger than the first");
    return n2;
  }else{
    alert("The two numbers are equal");
  }
}
alert(max(8,2));

1 Answer

Gabbie Metheny
Gabbie Metheny
33,778 Points

Your code is good, but you have some extra stuff in there that's throwing off the challenge!

Task 1 just asks you to return the larger of the two numbers, which you're doing in your return statements within the function, and then Task 2 only wants you to create an alert below the original function which will display the results of the function. Those extra alerts within the function are throwing off the challenge, just remove those and you should pass!