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 trialMaurice Hobbs Jr
5,278 PointsIm stuck on naming the function max
I am getting an error code
function max(length, width ){
if (either(length > width ) || (width < length )){
var answer = either
}
return anwer;
}
Maurice Hobbs Jr
5,278 Pointsthank you I changed but I'm still getting the same error
5 Answers
Amber Stevens
Treehouse Project Reviewerthis is what worked for me:
function max(length, width ){
if (length > width ) {
return length;
}
else if (width > length) {
return width;
}
}
Steven Parker
231,275 PointsThis 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
231,275 PointsMaurice 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
Treehouse Project ReviewerBut 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
231,275 PointsYes, I'd agree that is a correct solution. Good job!
Amber Stevens
Treehouse Project ReviewerSo 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
Maurice Hobbs Jr
5,278 Pointsit still didn't work but thank you I know I am now on the right track.
Amber Stevens
Treehouse Project ReviewerHmm.. 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
231,275 PointsI 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".
Maurice Hobbs Jr
5,278 PointsI had a couple words backwards and needed to delete a couple things so thank you.
Clark Reilly
6,204 PointsClark Reilly
6,204 Pointsto 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.