Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Functions Pass Information Into Functions Create a max() Function

I'm not sure what I did wrong I've been try for so long.

script.js
function max(num1,num2) {
return num1 + num2;
}
let num1 = 5;
let num2 = 4;

3 Answers

Currently, your function returns the sum of the two numbers rather than the maximum number. The challenge wants you to return the value for num1 if the value for num1 is greater than the value for num2 and otherwise return the value for num2. In your code, since num1 = 5 and num2 = 4, your function should return its value for num1, which is 5. If instead you had set num1 = 5 and num2 = 6, your function should return its value for num2, which would be 6.

Thanks I didn't realize what it meant!

Hey Jaelyn Fields,

The simple way to do this is by using a if else statement, this way it'll return the larger number out of two. The code will look like this:

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

Essentially you have to implement the Math.max() function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

Thank you I appreciate it!!