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

Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

Don't how to solve it

I can't solve the problem.

I don't know how to write a conditional statement where i should put the numbers in a second time.

I should put the if clause inside the function?

Cesare

script.js
function max(num1,num2) {
   return (num1,num2);
  if 
} 

3 Answers

Luis Ancheta
Luis Ancheta
6,435 Points
//create function named "max" which takes 2 parameters (num1 and num2).
function max(num1, num2){
//write a conditional statement which returns the larger number of the 2 parameters.
  if(num1 > num2) {  //if num1 is greater than num2
    return num1       //return num1
  }
  else{           //else return num2 (if the above condition is false, then num2 MUST be greater than num1)
    return num2 
  }
} 

Hope this helps!

Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

Thank you Luis

Yes is very helpful, but I'm still confused by the second step of the exercise.

It tells me to call the function and it could help Math.random()

I don't know how to write it

Exemple:

max (); in order to call the function

If i would put a number for the argument i would max (1,2)

but if i have to put a random number?

I really don't konw how to write it

Luis Ancheta
Luis Ancheta
6,435 Points
//Here you created a function named "max"
function max(num1, num2){
  if(num1 > num2) {  
    return num1      
  }
  else{ 
    return num2 
  }
} 
//To complete the challenge all you have to do is call your function.
// To call a function, you just use it's name! Example: max();   <== this calls your function, BUT remember that your function takes two parameters (num1, and num2). 
//With that in mind, when you call your function you must give it two parameters to work with. In this case you need to use two numbers. Any two numbers will work, and your function will return the larger one of the two.
//So when your call your function it should look something like this:

max(1, 2);

//I believe the example is asking to use the alert method to display the output of your function, so you might want to do this:

alert(max(1, 2));
Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

It's different,

It intended to write this, but i didn't know it was possible:

alert (max (Math.random(), Math.random()));