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

Mohsen Ahmad
Mohsen Ahmad
2,699 Points

max function help please. Isn't returning a number.

Here is the code I put:

''' function max(width, length) { if(width>length) { return width; }; } max(100,120); ''' Thanks!

script.js
function max(width, length) {
  if(width>length) {
    return width;
  };
}
max(100,120);

3 Answers

Shane Oliver
Shane Oliver
19,977 Points

In your logic you are passing 100 width and 120 length. Your function will only return width if it is bigger than length, which it isn't :) try swapping the numbers around to

max(120, 100) // this will return the 120 because width 120 is bigger than length 100
Mohsen Ahmad
Mohsen Ahmad
2,699 Points

hi. Thx. but still i get "The max function isn't returning a number." Shane Oliver

Shane Oliver
Shane Oliver
19,977 Points

The question asks you to alert the result of the max function

function max( w, h ) {
    if ( w > h ) return w;
    else return h;
}

alert ( max( 120, 100 ) );