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

Payal Sen
Payal Sen
1,286 Points

What is wrong with this code?

I can't figure out where is the mistake it keeps on asking if I have passed 2 number to my function but which is already there. Any help would be appreciated, is it a syntax error?

script.js
function max(a, b){

if (a>b){
alert(a + "is bigger than" + b);
return a;
}
  else
  {

alert (b + "is bigger than" + a);
    return b;
}


};

alert (max (12, 56));

1 Answer

First, you don't need to call the function, and second, you should have spaces at the beginning and end of the strings so that it wouldn't print something like "3is bigger than2" and instead "3 is bigger than 2".

Try this:

function max(a, b){
    if (a > b) {
        alert(a + " is bigger than " + b);
        return a;
    } else {
        alert(b + " is bigger than " + a);
        return b;
    }
}

Good luck! ~Alex