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

Ahkeem Lang
Ahkeem Lang
16,358 Points

I don't understand how I got this question correct??!! shouldn't both parameters 'bigger' && 'smaller' be relative

function max(bigger, smaller) { var number = bigger > smaller; return smaller if (bigger > smaller) { alert(bigger + "is larger than" + smaller); } else { alert("Something is wrong"); } }

max(45, 33);

^^^ "How is this correct??

bigger is relative to 45 and smaller to 33. but when I return the argument: "return bigger" it tells me I'm returning the smaller number out of the two?!

Jake Lundberg
Jake Lundberg
13,965 Points

I'm sorry, I don't understand your question...you said your code was correct, but you got an error saying you're returning the smaller number of the two? Also, I don't see in your code where you have specified returning 'bigger'? Could you please clarify?

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

You're right, your code is kind of working by accident. I put a couple comments in your code below:

function max(bigger, smaller) { 
  var number = bigger > smaller; // you create a variable here (which will be a bool), but don't use it later.  So this line is irrelevant
  return smaller // after you return something, we exit the function.  Nothing below this line gets looked at.
  if (bigger > smaller) { 
    alert(bigger + "is larger than" + smaller); 
  } else { 
    alert("Something is wrong"); 
  } 
}

So the central problem is that we don't actually know which of the arguments will be bigger or smaller, we just know that two numbers will be passed in. You called your parameters bigger and smaller in that order, but it won't necessarily be so. It just happens that the treehouse challenge test case passed in two arguments that met your assumption, and I guess they're not using enough of a variety of test cases to check whether you've beat the challenge by accident like you have.

Here's a solution that might make more sense:

function max(num1, num2) {
  if (num1 > num2) {
    return num1; 
  } else if (num2 > num1) {
    return num2;  
  }
}
Ahkeem Lang
Ahkeem Lang
16,358 Points

Thank you so much!