12. Functions & if/else // Unexpected token else syntaxError


#1

SyntaxError: Unexpected token else, what am i missing?

var sleepCheck = function(numHours) {
    if (numHours >= 8); {
    return "You're getting plenty of sleep! Maybe even too much!";
} else {
    return "Get some more shut eye!";
}
}
console.log(sleepCheck(10));
console.log(sleepCheck(5));
console.log(sleepCheck(8));

#2

else follows if, so whatever precedes your unexpected else is not an if


#3

Found the error,

if (numHours >= 8); {
Should have been
if (numHours >= 8) {


#4

When the condition in an if-statement is true, the following statement is executed.
If that statement is empty (semi-colon ends statements) then just that empty statement is executed.

{}'s are used to group multiple statements into one statement.


#5

This assignment did not work out for me until I added an else if statement. The else alone, following the if would not work.


#6

please share your code so we can see what you mean @kisabel86


#7

i added "else if" but still the same unknown syntax error.


#8

var sleepCheck = function(numHours);
if ( numHours >= 8) {
return "You're getting plenty of sleep! Maybe even too much!";
} else {
return " Get some more shut eye!";
}
sleepCheck(10)

can someone helping with the above code: it is saying " unexpected token" in the console and "something wrong with syntax down the bottom. i have been looking at these for ages trying to work out what mistake I have made. still cannot think.


#9

You don't have {} for the function. You should put { at the end of line 1 and }; before sleepCheck(10)


#10

getting annoyed here with an error message, I am getting the correct output as I change the numbers but the lesson won't allow me to proceed. here is my code:

function sleepCheck(numHours){
if(numHours >= 8) {
return "You're getting plenty of sleep! Maybe even too much";
}
else {
return "Get some more shut eye!";
}
}
sleepCheck(10);

my code prompts this error even though the output is correct: Oops, try again. It looks like sleepCheck() isn't returning "You're getting plenty of sleep! Maybe even too much!" when numHours is 10. Check your if / else syntax and whether you're using the correct comparison operator.


#11

I figured it out, it's because I didn't have an exclamation point after "much" in my if return string. jeez Louise! ...hopefully this is a lesson to someone else reading this. details are important