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

Andrew Boschee
Andrew Boschee
8,821 Points

not sure if I am incorporating the if and else if statement properly to return the high number

following off of the video, aside from the if and else if loops, but I'm not sure if I am properly declaring the highNumber variable or if properly trying to return the highNumber .

script.js
function maxNum (firstNumber, secondNumber){
 var highNumber;
  if {
    (firstNumber>secondNumber)
    return highNumber;
  }

  else if{
    (firstNumber<secondNumber)
    return highNumber;
  }

}
console.log(maxNum(10,20));

I think you're making it slightly too difficult on yourself. You don't actually need the var highNumber for this.

Second of all, an if conditional statement is made like this: if the firstNumber is bigger than the secondNumber, return the number that is bigger (firstNumber).

if(firstNumber > secondNumber) { 
   return firstNumber;
}

Now try and do the else if in the same way but with secondNumber being bigger than firstNumber. :) If you still can't find it, just tell me and I'll try to explain it more.

1 Answer

Steven Parker
Steven Parker
231,236 Points

Here's a few hints:

  • you don't need to create any new variables
  • you will return either firstNumber or secondNumber
  • you will compare them against each other to determine which one is larger
  • the conditional expression (in parentheses) of a "if" statement comes before the open brace of the code block
  • you can use a plain "else" (instead of "else if") to respond to all other conditions
  • a plain else does not have a conditional expression