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 Functions Pass Information Into Functions Create a max() Function

Jordan Branch
Jordan Branch
2,762 Points

SyntaxError: script.js: 'return' outside of function `r` (6:4)

I think I'm inputting something wrong or i got my parameters wrong?

script.js
function max(highNumber, lowNumber) {
  return highNumber;
}

if(highNumber >= lowNumber) {
    return true;
} else {
    return false;
}

1 Answer

That'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
Jordan Branch
2,762 Points

So instead of highnumber, lownumber i should make it something neutral?

Yeah, something more general, because lowNumber > highNumber logically doesn't make sense.

x and y, alpha and bravo, num1 and num2, names that are very general or uncertain. In this case I'd choose either num1 and num2 or number1 and number2.

Enlighten me if you come up with better names! Hope this helps!

there is no need of else after a return though.

You're right, very ignorant of me not to know the existence of >=.