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

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
215,954 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.