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

trying to create a function that returns the higher argument

so I am getting an error can someone point me to my error trying to make a function that allows two numerical arguments and returns the larger argument

script.js
function max( less, more ); {
 if(less > more) 
    return less;
    } else {
    return more;
}

3 Answers

Luciano Bruzzoni
Luciano Bruzzoni
15,518 Points

Hello, your logic is good, you just have a couple of typos.

the ; after the function's parameter should not be there, and you forgot to open a bracket after the if statement, and you are missing a close bracket at the end.

should be something like this:

function max( less, more ) {
    if(less > more){ 
        return less;
    } else {
         return more;
    }
}

good luck!

thankyou so much!

awesome thank you for your quick reply

geoffrey
geoffrey
28,736 Points

You could do that as well, It's called a conditional ternary operator.

function max( less, more ) {
   return (less>more) ? less : more;
}

The logic involved is the same as the code you typed, but in one line. If less is bigger than more, then return less, otherwise more.