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

Vasko Jocevski
PLUS
Vasko Jocevski
Courses Plus Student 1,040 Points

Need HELP !!!

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.

Can`t really understand !!!!

script.js
function max () {

}

2 Answers

Hello!

The function helps you check which of the two numbers is larger. Therefore, the function needs to know which two numbers to check. You need to allow the function to have two parameters. It currently doesn't have any. It would look like this:

function max(firstNumber, secondNumber) {

}

But it doesn't do anything yet.

You have to check which number of the two (firstNumber and secondNumber) is larger. How could we achieve that? Yeah, that's right! With a conditional statement.

function max(firstNumber, secondNumber) {
    if (firstNumber > secondNumber) { // we check if the first parameter is larger than the second one
        return firstNumber; // if it is, then we know which of the two is the maximum number
    } else { // it means that the first statement is false (firstNumber is NOT larger than secondNumber)
        return secondNumber; // then secondNumber is larger than firstNumber, so we return it
    }
}

Hope it helped!

The directions state to create two numbers for the argument, but only accepts words. Pure numbers do not work... Why is this?

Khaleel Yusuf
Khaleel Yusuf
15,208 Points

There are two things you need to do.

  1. You need to do is give the function 2 parameters ie.(num1, num2)
  2. You need to make an if statement to test which is the larger number I hope this helps, Reply if you still need help. Khaleel