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

Ryan Fox
Ryan Fox
4,971 Points

I'm stumped...

Not quite understanding this

script.js
function max(5, 10) {

}

2 Answers

Steven Parker
Steven Parker
231,236 Points

When you define a function, you would not use actual values as arguments. Instead, you give the arguments names, which you can then refer to inside the function as you handle the processing.

Then you still need to write the code inside the function. As the challenge hints, you'll need a conditional ("if") statement that compares the passed-in arguments. You might also need an else to go with it to handle the opposite condition. And as also specified in the challenge, there will be one or more return statements to pass back the correct value.

You're just putting together the basic things you've already passed challenges on. But if you're still stumped, you might want to review Getting Information From a Function, and/or Giving Information to Functions.

Ryan Fox
Ryan Fox
4,971 Points

Thank you for the feedback!

Ryan Fox
Ryan Fox
4,971 Points

Steven, I see you're from Baton Rouge! Nice to find a neighbor on Treehouse!

Steven Parker
Steven Parker
231,236 Points

:wave: Well, hey there, and congrats on being #1 on the local leaderboard for last week. :+1:

Hi Ryan,

You need to name two parameters that are passed into the function - I chose num1 and num2 because I lack imagination. Inside the function, see if num1 is bigger, if so, return it; else return the other.

That could look like:

function max(num1, num2){
  if (num1 > num2) {
    return num1;
  } else {
    return num2;
  }
}

Let me know how you get on.

Steve.

Ryan Fox
Ryan Fox
4,971 Points

Thank you for the feedback! I understand it now :)