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

Brian Patterson
Brian Patterson
19,588 Points

Not sure what is wrong?

Not sure what is wrong with this code?

script.js
function max(width, length) {
 if (width < lenght)
  return width;

}

have you assigned the perimeters?

1 Answer

Steven Parker
Steven Parker
229,967 Points

Well, you're part way there. You want to return the LARGER of the two, so you need to reverse your test. Then, you need to return the OTHER value if the test fails (using else):

function max(width, length) {
  if (width > length)  // if width is LARGER
    return width;      // ...then return width
  else
    return length;     // otherwise return length
}

You can actually omit the else if you want, since the function would have already returned in the other case.