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 trialMike 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:
-
max
is not taking 2 arbitrary numbers. If you pass20
and10
to your function, since20
is always greater than10
, your function will always returntrue
. To fix this, we should replace20
and10
with 2 generic names, such asa
andb
:
function max(a, b) {
if ( a > b ) {
// the rest of the code
- The function needs to return either
a
orb
. Right now, your function returns eithertrue
orfalse
. To fix this, simply replace them with eithera
orb
:
...
if (a > b) {
return a;
} else {
return b;
}
...