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
Mike John
3,182 PointsCan someone please help me with this challenge.?
Can someone please help me with this challenge.? I can't get my head around it.
function max (20,10){
if(20 > 10){
return true;}
else {return false};
}
1 Answer
Kip Yin
4,847 Points- Your code is syntactically wrong:
function max (20,10) {
if (20 > 10) {
return true;
} else {
return false;
};
};
Your goal is to return the larger of two numbers. That is, if you have two numbers a and b in general, your function max needs to return either a or b, whichever is larger. With this in mind, there are several problems with your code:
-
maxis not taking 2 arbitrary numbers. If you pass20and10to your function, since20is always greater than10, your function will always returntrue. To fix this, we should replace20and10with 2 generic names, such asaandb:
function max(a, b) {
if ( a > b ) {
// the rest of the code
- The function needs to return either
aorb. Right now, your function returns eithertrueorfalse. To fix this, simply replace them with eitheraorb:
...
if (a > b) {
return a;
} else {
return b;
}
...