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!

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

Create max function task

I've created the function and aded 2 parameters. I have an if/else statement to compare them but I get SyntaxError. I'm missing something but I don't understand what I've done wrong. Any help would be great.

function max(11, 87) {
  if (11 > 87) {
    return 11;
  } else {
    return 87;

3 Answers

Hi Chloe,

It seems seems you're hard-coding your code. A major point in programming is to create the most generic possible solution, so that multiple inputs can be processed the same way.

In your example, your function needs to take two variables as input, not predefined constants ( you have 11 and 87). When you're comparing your numbers, the assignment says you can name your variables whatever you want. While the body of your function is technically correct, it's not a generic solution to the problem.

Instead of using hard-coded integer numbers for parameters, I'm using variables named "first" and "second". Then, I'm deciding if the first number is greater than the second, to return the first variable. Otherwise I'm returning the second variable.

In the case you wanted to output, the numbers 11 and 87 would be input in to the function using a function call max(11,87) after the function's declaration.

So a passing input would be

function max(first, second) {
  if (first > second) {
    return first;
  } else {
    return second;
max(11, 87);
Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

The parameters are supposed to be variable names that will hold values you pass to them. They should not be values to start with. But the logic you have is solid. See my code for clarification. Hope this helps!

function max(x, y) {
  if(x > y) {
    return x;
  } else {
    return y;

Thanks for the answers I was heading the right direction with my first thoughts at least. John I did try something similar to start with yet still receiving an error obviously missing something but thanks for the clarification. I've struggled with learning functions in past thought it was going in perhaps not...

You're on the right track, absolutely. The best attribute of a programmer, at least in my opinion, is the willingness to try again. Asking questions alone is a step is the correct direction!

I re-read my reply and I think I might have seemed snarky. That reply was really just me trying to explain in a way that let me understand when I was in your position. It took me a while to figure out these core concepts as well. It seemed to come natural to everyone else, but it took me a bit more time to pick it up. I think if it was explained in that way at the beginning, it would have helped out greatly.

Hope this helped!