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

Ben Esther
Ben Esther
881 Points

I can't figure out this challenge

When I try this code it says "Syntax error: parse error."

  1. What does it mean by "parse error" and
  2. How should I fix my code?

Thanks!

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

2 Answers

andren
andren
28,558 Points

Parse error means that something in your code is syntactically incorrect, which leads to Javascript not being able to understand what you are asking it to do.

The part of your code that is syntactically incorrect is the parameters of your function. Parameters are used to pass values into a function when it is called, they are essentially variables that are assigned a value when the function is called. As such it's invalid to use actual values like strings or numbers as a parameter. A parameter has to be a variable.

The purpose of the Max function you are asked to create is to take two numbers that are provided to it when it is called, and then actually determine which one of those numbers are the largest through code, and return the number based on that.

Therefore the declaration of the function should look more like this:

function max(a, b) { // first parameter is a, second is b
    // a and b will now exist as variables within your function
    // Now you need to test if a is larger than b
    // Then return the number that is larger. 
}

You can't just provide your own numbers and then just hardcode the return like you have done in your solution, that would not make for a very useful function.

I have provided some basic instruction in the code example above, but if you have further issues completing the challenge then I can provide the solution for you, but I'd recommend trying to solve it once more on your own first.

Ben Esther
Ben Esther
881 Points

Thank you so much!

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Ben Esther

A parse error means that when your browser reads the JavaScript code, it doesn't understand it. Basically, it means that the code isn't written correctly. In your case, you're not using parameter names in your functions:

function max(9, 11) {

}

Is not a "legal" way of writing JavaScript code. The 9 and 11 there are "literal values" -- actual numbers. Functions require parameter names -- just like variable names. For example:

function max(num1, num2) {
  // num1 and num2 are like variables -- you "pass" values into those parameters when you "call" the function
}

max( 9, 11); // this is "calling" the function sending the values 9 and 11 to the function

You can then use those parameter names in your function to do something. That's a quick hint. If you can't figure out the code challenge on your own, I can help some more.

Dave McFarland
Dave McFarland
Treehouse Teacher

ha ha -- or just read andren's answer. It's much better than mine :)