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 trialJaime Rios
Courses Plus Student 21,100 PointsCan 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?
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
Courses Plus Student 7,254 PointsIt 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
Courses Plus Student 21,100 PointsJaime Rios
Courses Plus Student 21,100 PointsNope, 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?
Jon Kussmann
Courses Plus Student 7,254 PointsJon Kussmann
Courses Plus Student 7,254 PointsI will edit my post to show you what I mean.
Jaime Rios
Courses Plus Student 21,100 PointsJaime Rios
Courses Plus Student 21,100 PointsI got you. So the alerts should be always written outside the function?
Jon Kussmann
Courses Plus Student 7,254 PointsJon Kussmann
Courses Plus Student 7,254 PointsNot 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).