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

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?