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

Don't know why this code quiz wont work

I have to make a fucntion called max and take 2 numbers as arguments and then return the greater. i tried doing it with variables but still wont work

function max(10,9) {
if(10>9)
{
return 10;
}
}
script.js
function max(10,9)
{
  if(10 > 9) {
   return 10;
  }
}

Don't hardcode in the numbers but instead put in some variable names and compares those.

function max(number1, number2) {
    if(number 1 > number2) {
  return number1; 
} else{
return number2;
}
}

2 Answers

Grace Kelly
Grace Kelly
33,990 Points

Hi Kshitij, I would not put numbers into the function parameters, instead use variables e.g x and y, also you need to add an else if statement to check if the second parameter is greater than the first, like so:

function max(x,y) // use variables for parameters
{
  if(x > y) {
   return x;
  } else if (y > x) { //add else if statement to check if y is greater than x
    return y;

  }

}

Hope that helps!!

Thank you. Works well now