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

Jaime Rios
PLUS
Jaime Rios
Courses Plus Student 21,100 Points

Can you tell what is wrong with the code,

it says that I'm not passing 2 numbers to the max() function. But writting max(3, 5); should do that. Am I missing something?

script.js
function max(A, B) {

   if (A > B) {
      alert(A + " is larger than " + B);
      return A;
   } else {
      alert(B+ " is larger than " + A);
       return B;
   }

}

alert( max(3, 4) );

2 Answers

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

It looks like since you are calling the alert method with the max function inside (your last line), you do not need to write alerts inside of your max function.

Like this:

function max(A, B) {

   if (A > B) {
      return A;
   } else {
       return B;
   }
}

alert( max(3, 4) );
Jaime Rios
Jaime Rios
Courses Plus Student 21,100 Points

Nope, even if just write max(3, 6 ); in the last line it keeps saying 'Did you pass 2 numbers to the max() function'

How did you solve the challenge?

Jaime Rios
Jaime Rios
Courses Plus Student 21,100 Points

I got you. So the alerts should be always written outside the function?

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Not always, but it works well in this case. No matter the result from the max() function you want to alert/display the result, so it makes sense to to do like above. If you want to alert/display only if (for example) A > B then you can do so inside of the function... you just can't do both inside and outside of the function (as far as I know anyways).