Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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: