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

Can't seem to get this challenge to pass.

I'm a little stuck on this challenge. The challenge wants you to return the higher number .

script.js
function max(x,y){
  if(x >10 && y < 5){
   return true;
  }
  else{
    return false; 

}
max(11,2);

1 Answer

Stone Preston
Stone Preston
42,016 Points

the task states: Create a new function named max() which accepts two numbers as arguments. 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.

your function logic is incorrect. the max function needs to compare two numbers passed in to the function, and return the largest one. you are currently comparing the function parameters to two arbitrary numbers and returning a boolean, which is not what you are tasked with doing.

first, we need to see if the first parameter is larger than the second. if x > y, we can return x since its the largest. if its not greater (else) we can return y. Note that if the two arguments are equal, the second will be returned. That is fine for the intents of this challenge

function max(x, y){
  if (x > y) {
     return x
  } else {
    return y
  }
}