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

Below is the case. I not sure how to pass.

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.

function max(2, 5) { if(2 > 5) { console.log(true); } else { console.log(false); } }

script.js
function max(2, 5) {
  if(2 > 5) {
    console.log(true);
  } else {
    console.log(false);
  }
}

7 Answers

You need to return the larger number, not a true/false. And these need to be named parameters, so:

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

Thanks

Abe Layee
Abe Layee
8,378 Points

First of all, you need to create the max() function and pass it two arguments. Then compare those arguments with if else . Lastly, alert the max() funtion.

 function max(a,b){};

Compare a and b with if else statement

if(a>b) { // is a greater than b?
   return a;
}

 else {
   return b;
}
max(10,1)// 10 represents a and 1 is b...  now we pass the arguments to the function once we call it.

Lastly, we alert the max function using the alert method.

 alert(max(10,1)); //you can delete the above the else statement max() function  for the second part.

Many thanks. I passed.

Many thanks. I passed.

Abe Layee
Abe Layee
8,378 Points

glad I could help;D

(Y)