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

Isa Abo-Mohana
Isa Abo-Mohana
2,655 Points

what I'm doing wrong here?

i should be missing something but i don't know what

script.js
function max (long,long1) {
  if (long > long1) {
    return long;
  } else 
    return long1;
}
}
Steven Turner
Steven Turner
20,146 Points

You have an extra right bracket. When you are dealing with simple returns like that, it's just easier to drop the else statement as it's a little redundant.

function max(long, long1){
if(long > long1) {
return long;
}
return long1;
}

2 Answers

Steven Parker
Steven Parker
229,982 Points

You're missing a brace ("{").

The brace that should come right after the "else" to indicate the beginning of the code block is missing.

Another way to do it would be to remove the else and the following close brace, since when the if is true, the function will end (because of the return) before it goes any further.

You can use it on 2 ways

in your case you have too much }

function max(a, b) {
  return (a > b) ? a : b; 
}

is same as this:

function max(a, b) {
  if (a > b) return a;
  return b;
}
Steven Parker
Steven Parker
229,982 Points

The ternary operator may not have been introduced in the course yet.