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 trialAnusha Singh
Courses Plus Student 22,106 PointsI can't understand this question:- I typed { function max(a, b) { if(a > b) { return a; } }
Can you help me with how can I return the number for this
function max(a, b) {
if(a > b) {
return a;
}
}
3 Answers
anil rahman
7,786 PointsYou are correctly doing the check but the only thing you are missing is what to do when b is larger than a so basically an else clause.
function max(a, b) {
if(a > b) {
return a;
}
else{
return b;
}
}
alert(max(1,2));
anil rahman
7,786 PointsAnother way is this which is probably the better option than my first.
function max(a, b) {
if(a > b) {
return a;
}
return b;
}
Also you can do this:
function max(a, b) {
var result;
if(a > b) {
result = a;
}
else{
result = b;
}
return result;
}
brandonlind2
7,823 Pointsfunction max(a, b) {
var A;
if(a > b) { A= a;}
return A;
}
anil rahman
7,786 PointsThe return can be used within the if which is the correct the way for this question. Which i have posted above :)
brandonlind2
7,823 PointsI didnt know that buts thats nice to know
brandonlind2
7,823 PointsI didnt know that buts thats nice to know