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

Micah Dunson
Micah Dunson
34,368 Points

How to use a conditional statement to test 2 numbers to see which is the larger inside an argument of a function?

I'm not sure how to use a conditional statement to test the 2 numbers to see which is the larger of the two.

The problem states: create a new function named max() which accepts two numbers as argument. 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.

script.js

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi micah,

With this challenge, first you need to create the function that takes in two arguments. So the challenge wants it named 'max,' so you would declare 'max' as a function. var max = function(one, two) {}

After that, you'll need to use an if statement to see which number passed in is greater. Basically you are telling the program that if one is greater than two, return one. And if two is greater than one, return two.

Overall, your code should look like this:

var max = function(one, two) {
  if (one > two) {
    return one;
  } else {
    return two;          //you only have two variable to compare, so an if/else is all you need
  }
}

Hope that helps clear things up. Keep Coding! :)

Micah Dunson
Micah Dunson
34,368 Points

Thanks for the help, much appreciated!!