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

creating a function with two arguments

i don't understand what am doing wrong here but it is not calling the function max back and i tried calling it...

script.js
function max(firstNumber, secondNumber){
  firstNumber = 12;
  secondNumber = 5;
  if (firstNumber > secondNumber){
     return firstNumber;
  }else if{ 
      firstNumber === secondNumber;
   } else{
     return secondNumber;
   }
}

2 Answers

I think you delete the second third line. Don't use else if() statement. The first if() good, think about it, if the first statement is not true then a solution remains.

if (firstNumber > secondNumber){
     return firstNumber;
   } else{
     return secondNumber;
   }

or use shorthand

if (firstNumber > secondNumber){
     return firstNumber;
   } 
   return secondNumber;

Call the max() function outside of the max() function. Enter two parameters (two numbers), like this,

max(firstNumber, secondNumber);

Hope this help!

Thanks but my code can't find the variable

What kind of variable? No need to variable. Just call the max() function and give it two parameters, (two numbers), like this,

function max(firstNumber, secondNumber){
  if (firstNumber > secondNumber){
     return firstNumber;
   } else {
     return secondNumber;
   }
}
max(5, 9);

What happened now?

Thanks a lot it worked