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 trialJordan Branch
2,762 PointsSyntaxError: script.js: 'return' outside of function `r` (6:4)
I think I'm inputting something wrong or i got my parameters wrong?
function max(highNumber, lowNumber) {
return highNumber;
}
if(highNumber >= lowNumber) {
return true;
} else {
return false;
}
1 Answer
Tim Danner
3,284 PointsThat's a syntax error, the function is supposed to wrap the rest of codes in.
However, there's a few problems here. Your function is returning the first parameter and the rest of the codes are returning boolean
when it's supposed to return integer
. I would also consider renaming the parameters because we don't know which one is greater than the other:
function max(x, y) {
if (x > y) {
return x;
} else if (y > x) {
return y;
} else {
// executes only when both numbers are equal
return x; // or return y;
}
}
You can name the parameters num1
, num2
that's totally up to you, but highNumber
, lowNumber
would indicate that the first parameter should be greater and the second parameter should be less.
Jordan Branch
2,762 PointsJordan Branch
2,762 PointsSo instead of highnumber, lownumber i should make it something neutral?
Tim Danner
3,284 PointsTim Danner
3,284 PointsYeah, something more general, because
lowNumber > highNumber
logically doesn't make sense.x
andy
,alpha
andbravo
,num1
andnum2
, names that are very general or uncertain. In this case I'd choose eithernum1
andnum2
ornumber1
andnumber2
.Enlighten me if you come up with better names! Hope this helps!
Ella Ruokokoski
20,881 PointsElla Ruokokoski
20,881 Pointsthere is no need of else after a return though.
Tim Danner
3,284 PointsTim Danner
3,284 PointsYou're right, very ignorant of me not to know the existence of
>=
.