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

Challenge: create a max() function

I've tried many combinations still don't get it, do I use the if clause?

I tried to give each number a variable name, smaller=4, larger =7 then used the if clause.

If ( 4 < 7 ) then return true or return larger, and lots of different combinations!

script.js
function max( 4, 7 ) {
  var larger = 7;
  var smaller = 4;
  if ( 4  <  7 ) {
   return true;
  } 
}

1 Answer

Erik McClintock
Erik McClintock
45,783 Points

Mark,

You do want to use an IF statement, but you need to look at your parameters. Right now, you're hard coding the values 4 and 7 into your function, thus not allowing for arguments to be passed in when the function is called.

So, first, let's set up our function with parameters:

function max( numOne, numTwo ) {
}

Next, we want to check which of the two values passed in is larger, and then return that value:

function max( numOne, numTwo ) {
    if( numOne > numTwo ) {
        return numOne;
    } else {
        return numTwo;
    }
}

Hopefully this helps to make some sense of things. You want to make sure that you don't hardcode values into your function in this case so that you can call it and use it! Otherwise, this wouldn't be a very helpful function. When it is set up this way, you can call it and pass in whatever two numbers you wish.

Erik

thanks i'll give it a go