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

Ali Corlett
PLUS
Ali Corlett
Courses Plus Student 1,359 Points

create a new function named max which accepts two numbers...

I am looking at the code and it looks right... What syntax error am I making specifically?

script.js
function max (a, b) {

   if (a > b) 
   return a;
 } else  {  return b; 
   }
}

3 Answers

Your if statement is missing its opening bracket:

function max (a, b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

You could also use a shorthand tertiary syntax for such a simple if statement:

function max (a, b) {
  return a > b ? a : b;
}
Ali Corlett
PLUS
Ali Corlett
Courses Plus Student 1,359 Points

Thanks Matt. I think I looked at it for too long yesterday. I cannot pass the next section either. posted another:..

function max (a, b) {

if (a > b) { return a; } else { return b; }

} alert( max (a,b) );

I run this and I get, now No1 isnt passing. I go back. I change nothing, No1 passes. I am calling the function called 'max' and it has the two "numbers a, b". I really don't understand why the alert isn't showing a result as 'a'.

Ah! You just need to use actual numbers when you call the max() function inside the alert() method. Does that make sense?