"SyntaxError: Unexpected token else"


#1

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);

I was getting the same "It looks like sleepCheck () isn't returning..." message for awhile. After replacing all of the code, the error message is now "SyntaxError: Unexpected token else." I haven't been able to get around this error at all. What am I missing here?!

Continuing the discussion from Lesson 12: Functions & if/else:


#2

Hi you should close the sleepCheck() function }


#3

beside what @wizmarco already mentioned, you need to have opening { for your if statement


#4

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

This is my script for this lesson..hmm idk man I have the same error.
"SyntaxError: Unexplained token else"


#5

var sleepCheck = function(numHours) {
    if(numHours >= 8); // unnecessary semi-colon, dangerous semi-colon
        return("You're getting plenty of sleep! Maybe even too much!");
}; // unnecessary semi-colon
else {
    (numHours < 8); // else can't have a condition, since it is everything else, remove this whole line
        return("Get some more shut eye!");
}; // unnecessary semi-colon
// missing }; to close the function
sleepCheck(10);

see comments in code, there are quit a few problems with your code


#6

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);
sleepCheck(5);
sleepCheck(8);

I tried all the things you told me (i think) idk I'm just stupid or something I still have the same error :confused: I also tried just one curly bracket at the end instead of two and that didn't work either.


#7

Thank you very very much for such a reply btw :slight_smile:


#8

i missed it, i should have seen it:

if(numHours >= 8) // missing { to indicate the start of the if code 
        return("You're getting plenty of sleep! Maybe even too much!");
}

a if statement always look like this:

if (condition){
  // do something
}

i guess was a little distracted by the semi-colon there. If you add the { it should work


#9

Thanks! That fixed it :smiley: