Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Create a max() Function

Hui Zheng
PLUS
Hui Zheng
Courses Plus Student 1,380 Points

function max(big, small){ if(big < small){ return big; }else{ return small; } alert(max(12, 10));

I followed the instructions to write out this code but I kept getting the syntax error message. I have no idea of what I

script.js
function max(big, small){
  if(big < small){
    return big;
    }else{
      return small;
}
alert(max(12, 10));

3 Answers

rydavim
rydavim
18,798 Points

The parse error is due to missing a closing curly-brace at the end of your max function. Adding that in should get you to a more helpful hint about your solution on the challenge page.

function max(big, small){
  if(big < small){
    return big;
    }else{
      return small;
} // closes the else
} // one more here to close the function
alert(max(12, 10));

If you run into trouble though, let me know and we can walk through a solution. Happy coding!

Hui Zheng
Hui Zheng
Courses Plus Student 1,380 Points

function max(big, small){ if(big > small){ return big; } else { return small; } } alert(max(12, 10)); I revised the code to this, but afterwards I still received the syntax error.

Two things:

1) You are missing a closing bracket for your else statement

2) The function should return the larger of the two numbers.

Hui Zheng
Hui Zheng
Courses Plus Student 1,380 Points

Can't believe I had overlooked it. Besides the closing bracket everything else on the code was good right?

Thank you Maim

Hui Zheng
PLUS
Hui Zheng
Courses Plus Student 1,380 Points

It works now. Thanks so much for the help everyone