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

Anmol Bajaj
Anmol Bajaj
5,556 Points

Why it shows : There was an syntax error in parse.

I don't know why parseInt is showing error !!

script.js
function max(no1, no2){
  if (Number.parseInt(no1)>Number.parseInt(no2)){
    return no1;
  }else {
    return no2;
  }  

2 Answers

andren
andren
28,520 Points

You are missing a closing brace } at the end of your code to close the function. Also since this function will be passed in numbers, not strings, you don't need to parse them. Like this:

function max(no1, no2) {
  if (no1 > no2) { // No parsing needed
    return no1;
  } else {
    return no2;
  } 
} // Added missing closing brace

It's also worth mentioning that Number.parseInt is not supported in these challenges since it was introduced in JavaScript's ES6/ES2015 version which is only supported in some of the later challenges. The global parseInt function (without Number) is supported but as mentioned not needed for this challenge.

Anmol Bajaj
Anmol Bajaj
5,556 Points

It worked after I added a closing brace } at the end of the code. Otherwise, everything else was fine. Thanks for the help andren.

John Lack-Wilson
John Lack-Wilson
8,169 Points

Number.parseInt() takes two parameters, whereas you have just supplied one. See here on the documentation

andren
andren
28,520 Points

The second parameter is optional, so that would actually not cause any issues by itself.

Anmol Bajaj
Anmol Bajaj
5,556 Points

Yes andren is right, the second parameter is optional.

John Lack-Wilson
John Lack-Wilson
8,169 Points

I see, odd that the docs didn't specify optional, unless I mis-read

andren
andren
28,520 Points

The syntax example for the command is listed like this:

Number.parseInt(string,[ radix ])

Arguments enclosed in square brackets are optional, but it's true that it should have been specified next to the actual parameter description as well. Since MDN documentation is written by hand it is not always super consistent when it comes to things like this.

It's also possible it's not explicitly marked as optional because passing both arguments is the recommended way of using parseInt, since that reduces chances of bugs occurring form the wrong number base being assumed. Though in actual practice it's rare to see both parameters used.