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

david Ramirez
PLUS
david Ramirez
Courses Plus Student 2,798 Points

idk what i am doing

please

script.js
function max(parameter1 , parameter2){
  return parameter1 + parameter2;
}

2 Answers

Julian Gutierrez
Julian Gutierrez
19,201 Points

You need to return the larger of the two numbers, in this example you need to use an if/else statement inside of the function to accomplish this.

nulled
nulled
1,890 Points

You're close. Go step by step of what the task asks you to do.

We must create a function named max that takes in two arguments:

function max (parameter1, parameter2) {
}

The function should then return the larger number of those two numbers. It notes we must use a conditional statement. If-else is a conditional statement.

So we are doing a simple comparing of the two numbers to determine which one is greater. We do that by using a > or < operators. So here we have it:

function max(parameter1, parameter2) {
    if (parameter1 > parameter2) {
        return parameter1;
    } else {
      return parameter2;
    }
}