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

Enemuo Felix
Enemuo Felix
1,895 Points

Task challenge

someone should please help me out here. I don't mind a much detailed help too

script.js
function max (5,3) {
}
 max ('May','March');
if ('May' > 'March') {
  return
} else

2 Answers

Hi there,

In this challenge, all we need to do is write the function - we don't need to call it or anything. That means everything we do can be inside the function's brackets. The directions say we need to take two number values, so let's start with a basic function framework that takes two values:

function max(num1, num2) {

}

From here, we know we want to return the larger of the two values. So, let's test if the first value is greater than the second one, and return the first value if so:

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

Now we just need to return the second value if the first value isn't bigger. So, we add an else statement:

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

That should be what you need to pass the challenge - let me know if anything still doesn't make sense!

Enemuo Felix
Enemuo Felix
1,895 Points

Thanks Katie You have been wonderful!

Thanks - glad it helped!