Intro to JavaScript Sleep Debt Calculator

Scratching my head on this one. Used if/else statement. I know when you using ‘else if’ it must start with an if and end with an else. However when I got to the end and went to console log I found if I had curly brackets wrapped around “return 3;” for the final else statement I’d get an error saying unexpected token.

Not sure if I missed it or if I’m drawing a blank right now but do you not need to have curly brackets wrapped around the final return statement?

const getSleepHours = day => {
if (day === ‘monday’) {
return 9;}
else if (day === ‘tuesday’){
return 8;}
else if (day === ‘wednesday’){
return 7;}
else if (day === ‘thursday’){
return 6;}
else if (day === ‘friday’){
return 5;}
else if (day === ‘saturday’){
return 4;}
else (day === ‘sunday’) //{}
return 3;
};
console.log(getSleepHours(‘saturday’));

After removing the comment, the unexpected token is }.

Thanks for the response,

I understand that the unexpected token is }.

I was just asking at the end of an if…else statement do you not need curly brackets surrounding the finale return?

Thanks.

For consistency, yes.

else {
    return 3;
}

Technically, when each clause contains a return we don’t need else at all, and we can relax the syntax, slightly:

if (a) return b;
if (c) return d;
if (e) return f;
return g

You actually did well :), just a else statement you need to put the curly braces as well. Keep coding!

1 Like