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 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!!