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

When I "Check Work," the response is, "It doesn't look like you have a function named max()." Is it not named max?

I have been working on this question for two days. rewatched videos and watched youtube videos trying to figure it out. I wouldn't ask if I wasn't exhausted. However, I keep getting the response, "It doesn't look like you have a function named max()." I think I honestly have it named max and with two parameters. I have probably tried 100 different combinations and that is the response I get. I am sure the code is far from correct, but it is not due to lack of effort. Thank you in advance for your time and help! function max (bigNumber, littleNumber) { return max; } var max = 10; var bigNumber = 10; var littleNumber= 6; if (max>= 10) { console.log ( "Big Number"); } else { console.log ("Little Number"); }

script.js
function max (bigNumber, littleNumber) {
return max;
}
var max = 10;
var bigNumber = 10;
var littleNumber= 6;
if (max>= 10) {
  console.log ( "Big Number");
} else {
  console.log ("Little Number"); 
}

Thanks a lot for your help. My first tries were a lot closer to this. After a while, my mind went crazy. Thanks again.

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

The problem is you are immediately overwriting the function with a value of 10 with the line var max = 10; JavaScript makes no distinction between variable names and function names. In fact you can assign functions to variables, and call them with the variable names. The only difference is when you call the function with the parentheses after the name, like max().

function max (num1, num2) {
  if(num1 > num2) {
    return num1;
  } else {
    return num2;
  }
}

// you can assign function to a varaible
var someFunction = max;
// then call it like a function
console.log( someFunction(2,3) );
// is the same as 
console.log( max(2,3) );

Thanks a lot for your help. My first tries were a lot closer to this. After a while, my mind went crazy. Thanks again.

Rohit Tolawat
Rohit Tolawat
8,277 Points

Hi Chris, Your logic seems to be flawed. The issue with your code is as follows:

  1. You have declared the function max but have not called it anywhere in the code.
  2. Even if you call the function max in the fashion max(20,30), the result would still be 10. This is because there is no manipulation code in your function. It just returns max. The var max is globally scoped and its valued is initialized to 10. 3.Seth Kroger's solution seems a lot more logical if thats what you want to achieve.

Please let me know if you find this helpful.

Thanks, Rohit.

Thanks for your help and explanation.