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

Whatt am i supposed to do here

Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would like). The function should return the larger of the two numbers.

HINT: You'll need to use a conditional statement to test the 2 parameters to see which is the larger of the two.

script.js
function max(5, 10) {

  if(5 >= 10) {
     return 5;

   else if (5 <= 10) {
     return 10;
   }
  }else()
}

3 Answers

You are quite close to the solution but, instead of using fixed numbers, you need to use variables, so you can pass the numbers to the function parameters as arguments.

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

you also had a missing } in your first part of the if statement.

You don't need to compare the numbers again with an else if because if they failed in the first part of the if, you know that num2 is bigger.

function max(a, b) {
   if  (a >= b) {
    return a;
   } else {
    return b;
   }
}
Steven Parker
Steven Parker
231,275 Points

:warning: Explicit code answers without any explanation are strongly discouraged by Treehouse, and subject to redaction by moderators.

yeah, you are right, but the code is self explanatory.