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

How do I do this challenge task?

I'm completely stumped on this challenge. So far I've only been able to make a function and pass it 2 arguments, but I can't seem to get any further. Could someone please help with this? Thanks!

script.js
function max(5, 10) {
}

1 Answer

Per Karlsson
Per Karlsson
12,683 Points

Hi Gabriel,

The challenge wants you to use conditional statements (e.g. IF) to see which number is the largest.

So if a is larger than b, return a. Else you want to return b

Here's some code for you :)

function max(a, b) {
  if (a > b) {
    //Insert return statement here.
  } else {
    //Insert return statement here.
  }
}

Good luck!

~Per

Ok, that did help a bit. But I'm still getting syntax errors when I insert 5 for a and 10 for b in the function arguments.

Per Karlsson
Per Karlsson
12,683 Points

Hi Gabriel,

The challenge is just for the function declaration.

It's when you want to use / or call the function you pass in the argument. Like in the next stage of the challenge.

You could for example do something like this:

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

alert(max(5,10));

Hope this sheds some light on it :)

Good luck!

~Per