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

My Creation
My Creation
1,289 Points

I'm a bit confused on how to return the greater value of 2 numbers in parameters with an if statement.

Can someone please help with this function that must return the greater value of 2 number parameters with an if statement.

script.js
function max(2,5) {

}

2 Answers

You can use conditionals to accomplish this task.

But, before we can jump into that, we must be aware the you can't name parameters numbers. Apparently you called the first parameter 2 and the second parameter 5. Remember that in JavaScript parameters are like variables. You only give the values when you call the function, though. You name the parameters in the function definition (in your code it's the very first line of code where you define the function)

Once you've done that, you can write the conditional to test if the first argument is greater than the second argument, and if so, return the first argument (since it's bigger). Otherwise you should return the second parameter, since it's guaranteed bigger then the other. It could be the same, but if the parameters' values are the same, it doesn't matter which one you return.

A little free hint to help you get started:

function max(a, b) {
  if (a > b) {
    // Replace this comment with the code you should use...
  } else {
    // Replace this comment with the code you should use...
  }
}

I hope this helps :grin:

Good luck coding :sparkles: :+1: :sparkles:

:dizzy: ~Alex :dizzy:

My Creation
My Creation
1,289 Points

Ah, I totally forgot you couldn't use numbers, thanks for putting this into perspective!

No problem :smile:

I'm happy to help! :+1:

Congrats on solving the rest of the code on yourself! :tada: :confetti_ball: :tada: