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

kingdavid igbayilola
kingdavid igbayilola
8,678 Points

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

Adam Beer
Adam Beer
11,314 Points

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!

kingdavid igbayilola
kingdavid igbayilola
8,678 Points

Thanks but my code can't find the variable

Adam Beer
Adam Beer
11,314 Points

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?