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

Val Nunez
Val Nunez
2,763 Points

Shoot, this one is too tough for me. I watched all the videos repeatedly but I still don't get it, sorry.

I can't figure out how to format the conditional statement. Also the difference between parameter and arguments is still confusing to me and need some help distinguishing the two, please.

Thank you very much in advance!

script.js
function max(100, 10) {
  if (max < 10) {
} 
  else {
}
  return max
}

Hi Val, to format the conditional statement you want to place some code to run if the condition is met (in this example your condition is 'if the value of max is less than 10'). You would place the code that needs to be executed in between the curly brackets if max is indeed less than 10.

  if (max < 10) {
*your code to execute if max is less than 10 goes here*
} 

If max is not less than 10, then you're telling the browser to ignore the if code, and to instead execute the code that is inside the else curly brackets.

  if (max < 10) {
*your code to execute if max is less than 10 goes here*
} else {
*your code to execute if max is not less than 10 goes here*
}

2 Answers

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Here is the answer I came up with:

function max(first, second) {
  if (first > second) {
    return first
  } else {
    return second
  }
}

First, you are using max is if it where an argument. max is not an argument, it's the name of your function, so you can't use it in the body of your function (unless you named a a variable or argument max).

And if I am correct, you can't use numbers as argument names.

Val Nunez
Val Nunez
2,763 Points

Oh my god! Now I get it! Wow, thank you for your time and patience! I think I tend to overthink and then just myself up real bad. Thank you very much, Caleb!

ALFRED MOHENU
ALFRED MOHENU
1,499 Points

I don't really get the first part of your question. Could you please through more light on that. Now to your second question, let's say we have called Multiply as shown below which will multiply two values.

function Multiply(){};

Keep in mind that at the time of creating the function, we don't know the two values that we will be multiplying. We just want to create a function that can multiply two values. Because of this, when we create the function, we will have to pass in two parameters (letters or words) into the function that serve as variables(boxes) to store the values that we will be passing into the function to perform the required work. So now our function will look like something like this

function Multiply(value1 , value2){};

Now that we have created our function with our parameters which serve as placeholders or "boxes" for our values that we will be entering later in the future, we can now work with our values. So now our function will look like this

function Multiply(value1 , value2){

return value1 * value2; };

Now we have our blueprint ready, and anytime we want to multiply two "real" values, we can call our function and pass in the "real" values as arguments into our function. So we get to call our function like this

Multiply(2,3);

So in short, parameters are placeholders and arguments are the "real" values.

Hope that answered your question. :-)

Val Nunez
Val Nunez
2,763 Points

Wow, now I get it! So helpful, so clear - I ACTUALLY get it now! Thank you for your help and confidence!