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 trialVivienne Kay
1,708 PointsStuck trying to pass number values into a function
function max (a, b) {
if(a>b) return a;
else if(b>a) return b;
}
max(5, 8);
alert( max( ) ):
The alert is asking me if I passed the numbers correctly into the max function. Not sure what I'm doing wrong.
function max (a, b) {
if(a>b) return a;
else if(b>a) return b;
}
max(5, 8);
alert( max( ) );
2 Answers
Jonathan Dewitt
8,101 Points1) You have a bit too much logic. You don't need the second if
statement, as you only have two outcomes.
2) You should be passing the values directly into the function within the alert
function max(a, b) {
if( a > b )
return a;
else
return b;
}
alert( max(5, 8) );
Alternatively, you could put the alerts directly into the function and then run it by itself:
function max(a, b) {
if( a > b )
alert( a );
else
alert( b );
}
max(5, 8);
Vivienne Kay
1,708 PointsWow! Thank you so much! This fixed the problem and also helped me see how to write cleaner conditional statements in the future. ?
Jean Mesa
2,251 Pointsreplace the colon with a semi-colon on your last line
Vivienne Kay
1,708 PointsThanks Jean!
Jean Mesa
2,251 PointsJean Mesa
2,251 Pointsreplace the colon with a semi-colon on your last line