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

Dave Taylor
Dave Taylor
4,309 Points

Why do I continue to receive 'SyntaxError: Parse Error'?

I am completely stumped. I have no clue as to what I'm doing wrong to receive this error. Though, if I'm being honest, I have no clue how to complete this challenge either.

script.js
function max(314, 290) {
  if(314 > 290) {
  return 314
  } else {
    return 290
   }
}

1 Answer

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 (the parts inside the parenthesis) 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. You can't just provide your own numbers and then hardcode the return like you have done in your solution, that would not make for a very useful function.

The function should look like this:

function max(a, b) { // First parameter is a, second is b.
  // a and b will now exist as variables within your function.
  if(a > b) { // So you can use them in your comparison
    return a; // If a is the greatest then return it.
  } else {
    return b; // If a is not the greatest then b must be so return it.
   }
}
Dave Taylor
Dave Taylor
4,309 Points

Thank you for your thorough and timely response! You have helped me better understand parameters, and I appreciate the time you took to help me do so. I hope you have a terrific day.