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

need help

It's saying it's an error in my function but can't spot it. Am I'm missing something?

script.js
function max (upper){
  var max
return max;

}

1 Answer

Steven Parker
Steven Parker
231,271 Points

The instructions say to create a function "which accepts two numbers as arguments"*, but the function shown here takes only one argument.

The instructions also say "The function should return the larger of the two numbers", so you'll need some kind of comparision to control which argument to return.

Another hint: you won't need to create any new variables inside the function.

I’ve put

Function max (7 > 5) { Return upper;

I replaced β€œupper” and added two numbers arguments, also made the return value as 7 so it know which number is greater. When I rechecked the work it popped up as error, am I’m on the right track?

Steven Parker
Steven Parker
231,271 Points

In the definition, the function arguments are placeholders for actual values that will be supplied later when the function is invoked. They are given names which will work like variables inside the function body.

Then, any operations performed on or with them will occur in the function body.

LIke this?

Function max (width > height) { Return width; }

Steven Parker
Steven Parker
231,271 Points

Not quite. Remember I said that "any operations performed on or with them will occur in the function body". Plus, you'll need some way to return one argument or the other depending on which is larger.

It might help to look at some of the other questions asked on this same topic.

I've figured it out it was function max (width, height) {

return height; }

Steven Parker
Steven Parker
231,271 Points

I guess he challenge isn't testing very thoroughly! That should not have passed because it's still missing a comparison test and a separate return for when the other value is larger.