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

Tricky question

So I've follow step by step to properly do this function and I cam across the alert section and I follow each step and shows error on the screen, did I miss something?

script.js
function max (width, height){

return height;
max( width, height)
  alert( max (width, height ));
}

2 Answers

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,850 Points

Hey Qasim,

You need to check if height is greater than width, and return which is larger. To do that you need to compare the two values. You will also need an if statement.

Max Botez
Max Botez
5,146 Points

Hi Clayton, Look, I struggle to do this challenge as well. Look at my code, please.

function max (upper, lower){   // I declare the function and set some 2 parameters
 if ( upper > lower ){    // then I know that I need to use condition statement
}
return  //here I use return, to return the values 
}
max(3, 6);  // calling a function with comparing values 

here it seems to be ok, but it's not working :( or I miss something

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,850 Points

Hey Max,

You need to return which is larger. To do this, you could add a variable to assign the max value, or you could return from within the if clause. You will however need an else clause. Basically, if upper is greater than lower, return upper, else return lower.