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

Stephanie Raymos
Stephanie Raymos
3,124 Points

PLEASE help. Console says firstNum is not defined in line 6 Challenge Task 1 of 2 Create a new function named max whic

PLEASE help. Console says firstNum is not defined in line 6

script.js
function max( firstNum, secondNum ) {
  var largeNum;
  return firstNum + secondNum;

}
 if ( firstNum > secondNum ) {
   largeNum === firstNum;
 }else{
  largeNum === secondNum; 
 }
console.log( max (4,5) );

3 Answers

As Steven points out, firstNum and secondNum have only been defined inside your function. Your conditional (if/else) logic is outside the scope of the function you created, and will not know what firstNum (or any variable it's referencing). You need to move your if/else logic to inside the max function for firstNum and secondNum to be assigned. Also, make sure the end of your function is only returning the larger number.

Steven Parker
Steven Parker
231,271 Points

The testing code is currently outside of the function (where the parameters are not defined). The "if" and "else" code needs to be moved into the function, and before the "return" line.

You'll also want to change what the "return" is passing back.

Stephanie Raymos
Stephanie Raymos
3,124 Points

Thanks guys. I edited it to this:

function max( firstNum, secondNum ) { var largeNum; return secondNum; if (firstNum > secondNum) { largeNum === firstNum; }else{ largeNum === secondNum; } }

console.log( max (4,5) );

And it went through and said well done but I'm still wondering how it will return the first number if it happens to be larger.