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

Kevin Jervis
Kevin Jervis
2,600 Points

Create a Max Function

Hi Guys

Receiving a syntax error. Not too sure why.

Thanks Kevin

script.js
function max (h1, h2)
  if (h1 > h2) {
return h1; 
} else{ 
return h2;
}
alert (max (2,3) );

2 Answers

Christopher De Lette
PLUS
Christopher De Lette
Courses Plus Student 7,139 Points

Hi Kevin,

You have the code block correct in it's logic, it's syntax that is causing this error. A few curly brackets and you're good to pass the challenge. The function itself has to start with an open curly brace right after the method and just after the conditional another opening brace (which you have.) Simply close any remaining open brackets and you're golden. Here is the code so you can have a better visual understanding.

function max(arg1, arg2){
  if (arg1 > arg2) {
    return arg1;
  }
  else {
    return arg2;
  }
}

alert(max(2,3));

Take care and Happy Coding!
Kevin Jervis
Kevin Jervis
2,600 Points

Hi Christopher

That's awesome. Thank you I appreciate the help :o)