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 trialZoe Campbell
6,288 Pointswhat am i doing wrong?
what am i doing wrong?
function max(number1, number2) {
return = highestNum;
Var highestNum = if (number1 >= number2) {
return number1;
} else {
return number2;
}
Steven Parker
231,275 PointsBrian, the conditional part of the code is correct, because when they are equal it does not matter which one is returned. Functions can only return one value, and only the first return statement will be performed as that ends the function.
3 Answers
Steven Parker
231,275 PointsHere's a few hints:
- Since return is a reserved word, you can't use it as a variable name or assign anything to it.
- you can't reference highestNum before creating it
- the keyword "var" must be all lower-case
- you can't assign the result of an "if" (but don't need to anyway)
- you don't need to create a new variable for this challenge
- the function is missing a closing brace ("}") at the end
Zoe Campbell
6,288 PointsI got a bit confused but it worked out with:
function max(number1, number2) { if (number1 >= number2) { return number1; } else { return number2; } } <----this last curly brace is easy to forget!
Steven Parker
231,275 PointsIf you adopt the coding style of indenting each block a bit, it will be much more obvious when a brace is missing.
Zoe Campbell
6,288 PointsGood tip. Thanks!
Brian Ball
23,661 PointsBrian Ball
23,661 Pointsyou shouldn't be using >= (this will return number1 if it's equal to number2)
also, you should check for == or === ( higher, equal, or lower ) right?
if they're equal, do you want to return both of them? (as they are both the max and both the min)