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

Im stuck on naming the function max

I am getting an error code

script.js
function max(length, width ){
if (either(length > width ) || (width < length )){
var answer = either
}
return anwer;
}
Clark Reilly
Clark Reilly
6,204 Points

to start, you need to return anSwer, not anwer. But after fixing that, your code still doesn't work and I don't know jack about javascripts syntax, which is why this is a comment and not an answer. According to the compiler, you didn't declare a function named "max()" Mabye you messed up on something there.

thank you I changed but I'm still getting the same error

5 Answers

Amber Stevens
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Amber Stevens
Treehouse Project Reviewer

this is what worked for me:

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

}
Steven Parker
Steven Parker
229,982 Points

This would come close, but the "else if" isn't needed. Plus, if the two values are ever the same this code won't return anything. Having the second "return" stand alone will guarantee a correct return in every case.

Steven Parker
Steven Parker
229,982 Points

Maurice Hobbs Jr β€” please don't mark this as "best answer" .. it is not a correct solution even if it passes the challenge. But Amber has left another answer with a correct solution.

Amber Stevens
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Amber Stevens
Treehouse Project Reviewer

But yes, based on what you said, Steven Parker I found this also worked and is probably more what the challenge was looking for:

function max(length, width ){
  if (length > width ) {
            return length; 
  } 
              return width; 

}
Steven Parker
Steven Parker
229,982 Points

Yes, I'd agree that is a correct solution. Good job! :+1:

Amber Stevens
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Amber Stevens
Treehouse Project Reviewer

So the problem seems to be that you're testing them both in the same statement rather than testing them separately. The variable "answer" as you have it now is basically returning a boolean value of true rather than a number which is what the challenge is looking for. The way that I did this was I tried checking if length is greater than width then return length. else if width is greater than length, then return width

it still didn't work but thank you I know I am now on the right track.

Amber Stevens
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Amber Stevens
Treehouse Project Reviewer

Hmm.. I might have gone about it a different way I guess, but what I have there DID in fact pass the code challenge :)

Steven Parker
Steven Parker
229,982 Points

I believe it, some challenges test more thoroughly than others. But you can see that it's important to always return one value or the other. If this were a complied language, you'd most likely get a compiler error "not all code paths return a value".

I had a couple words backwards and needed to delete a couple things so thank you.