Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

stuck on challenge to create new function named “max” (to compare 2 numbers)

I have become stuck with this challenge, even after reviewing posts from others seeking help.

My guess is that my conditional statement is incorrectly formatted (and possibly other errors as well). Could someone please look at my code and help me find my mistakes?

Here is my code:

function max (numberA, numberB){
  if (numberA < numberB){
    return numberA
    else 
      return numberB
}
  return max (5,8);
script.js
function max (numberA, numberB){
  if (numberA < numberB){
    return numberA
    else 
      return numberB
}
  return max (5,8);

3 Answers

Alexander Solberg
Alexander Solberg
14,349 Points

1) You are using "return" to call the function, which ufortunately is incorrect usage of "return". "return" is used to "send back" a value from the function. If you want to use a function, you need to call it with parenthesis. so:

max(5, 8)

2) your "else" statement is missing its braces, {}

Other than that, it looks fine :)

function max(numberA, numberB) {
  if (numberA < numberB) {
      return numberA;
    } else {
      return numberB;
    }  
}

max(5,8)
Antonio De Rose
Antonio De Rose
20,882 Points
/*2 issues
issue number 1 -> if and else, where are the proper curly construction
there are two opening curly to one closing curly, in your code
there should be three opening curly to three closing curly, altogether considering the function
issue number 2 -> check, are you returning the smaller or the larger number*/
function max (numberA, numberB){
  if (numberA < numberB){
    return numberA
    else 
      return numberB
}
  return max (5,8); // why this statement, take it off, for the first task

Hi Antonio,

Thanks for your notes and suggestions for fixing the errors in my code. They are very helpful!

Hi Alexander,

Thanks for reviewing my code and for your suggestions, which are great. When I tried submitting this code, I got an error message telling me the smaller number was being returned (sorry, but I didn't think to copy the error message).

I changed the code to read as follows:

function max(numberA, numberB) {
  if (numberA === numberB) {
      return numberA;
    } else {
      return numberB;
    }  
}

max(5,8)

Fortunately, this change did the trick :). Thanks for your help!

Alexander Solberg
Alexander Solberg
14,349 Points

Ah I see where it went wrong. I am using the wrong comparison operator, "<", instead of ">".

I suggest sticking to ">" rather than "===". Because with the triple equals, you are checking if numberA is equal to numberB, which for some reason passes the challenge :P but it's not actually doing what was asked in the challenge.