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

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Create a max() Function

Zoe Campbell
Zoe Campbell
6,288 Points

what am i doing wrong?

what am i doing wrong?

script.js
function max(number1, number2) {
 return = highestNum;
 Var highestNum = if (number1 >= number2) {
  return number1;
} else {
  return number2;
}
Brian Ball
Brian Ball
23,661 Points

you 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)

Steven Parker
Steven Parker
231,275 Points

Brian, 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
Steven Parker
231,275 Points

Here'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
Zoe Campbell
6,288 Points

I 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
Steven Parker
231,275 Points

If you adopt the coding style of indenting each block a bit, it will be much more obvious when a brace is missing.