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

I don't understand this challenge.

i don't feel that i learned how to do this part with the previous videos.

3 Answers

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hey,

Well allow me to teach it to you now.

Create a new function named max() which accepts two numbers as arguments. The function should return the larger of the two numbers. You'll need to use a conditional statement to test the 2 numbers to see which is the larger of the two.

Let's break that down. Ok... So create a new function.

function max() {

}

Ok cool - Now, it takes to Strings. Ok no problem.

function max(num1. num2 ) {
}

Ok so creating a new function you use the keyword function. Then the name which is max. A function is followed by (). You put the arguments inside the (). That is what information or data the function is getting.

Ok, now it says return the larger of the two. Ok So I need a return statement. But return what?

I need to return the higher of the two numbers. Ok so I know I need a conditional statement. meaning I need an if statement. Ok let's try that.

function max(num1, num2) {
  if (Something is more than another) {
    return one or the other.

Ok cool. So I need a conditional statement so I use an if. Then I need to use a conditional operator. In this case I want to see if a number is more than another. Ok so I can use < less than or > greater than.

Ok so I need to find out if num1 is greater than or less than num2. Then I need to return the right answer.

If num1 is larger than num2. return num1. But if num1 is not larger than num2, I want to return num2. So you do if/else. Just like the example below.

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

Ok, I hope I didn't confuse you more with this, but if you have more questions. Please do ask and I will try to address them.

Let me know!

\Thanks

Abe Layee
Abe Layee
8,378 Points

The challenge is asking to create a max() function that takes two parameters. Think of the value passed in the function as variable. You can add pass more values into the paramater. The value you passed in the paramater are follow in order. In this example, the value a is 3 and b is 4. Is 3 > 4? All you have to do is passed a number at the end of the function and compare them.

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

alert(max(3,4));

Thnks both for you help!! What i had done before was this exactly.

function max(3, 4) { if (4 > 3) { return 4; } else { return 3; } };

Ryan Ruscett
Ryan Ruscett
23,309 Points

So are you saying you are still having an issue or are you just letting us know how you did it before but you are all set now?

I was letting you know how i did it before and I'm all set now