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

Justin Warren
Justin Warren
7,805 Points

I am a bit confused about where to go on this one. In particular the format I need for the conditionals

Any help appreciated!

script.js
function max(height, length)
if (height > length) {
  return height
} else (length > height) {
  return length
}

1 Answer

Damien Watson
Damien Watson
27,419 Points

Hi Justin,

You need the function to return either the 'height' if larger or the 'length'. So you would need an 'if then else'. If the height is not greater than the length, then the length should be returned.

function max(height, length) {
  if (height > length) {
    return height
  } else {
    return length
  }
}

If you wanted to check multiple , the correct syntax is below, but it is good practice to return a default if something is expected to be returned. You can chain together as many as you like, but if you have that many, then maybe a 'switch' statement would work better, but that may be in another lesson.

function check(a,b,c) {
  if (a > b) {
    return "a is greater than b";
  } else if (a > c) {
    return "a is greater than c but not b";
  } else {
    return "a is neither greater than b or c!";
  }
}