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!

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

Returning Functions w/Conditional Statements

Create a new function named max which accepts two numbers as arguments. You can name the arguments whatever you like. The function should return the larger of the two numbers.

you'll need to write a conditional statement to test the two paramenters

script.js
function max (25, 26) {
if (25<26)
  return 26;
}
else (
 return 25;
)

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Your code is not really wrong, but you're hard-coding the values. The guys and gals over at Treehouse are going to send two unknown numbers to your code to check it. So you have to accept whatever they're sending and evaluate that. In short, your code must be more generic.

function max(a, b){
  if(a > b) {
    return a;
  } else {
    return b;
  }
}

I declare a function max and accept two numbers. These two numbers are now assigned to the variables a and b (inside that function). If Treehouse sends 10 and then 20 then a will be 10 and b will be 20. Now I look and see if a is bigger than b. If it is, then I return a. Otherwise I return b. In our example b (or 20) will be returned. Hope that clarifies things!