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

function can't find the variable

I don't understand why my code can't find the variable but looking at it , i can't find anything wrong

script.js
function max(firstNumber, secondNumber) {
var firstNumber =12;
var secondNumber = 5;

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

1 Answer

Niclas Hilmersson
Niclas Hilmersson
8,296 Points

Ok what you need to do is remove those variables. the function uses the two parameters inside of the first function call to get the numbers.

When you call the function like this max(12, 5);

that's gonna set the numbers for the parameters that you have set as variables. Therefore you don't want to create any variables inside the function. imagine that the numbers written inside of the function call at the end. Replaces the parameter of firstNumber and secondNumber

function max(firstNumber, secondNumber) {


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