Functions & if / else


Hi, I'm am really stuck. When I check this function it tells me "SyntaxError: Unexpected keyword 'else'" When I delete the "else" it runs the function until it has to check 5 hours.

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!";


Semicolon after if (or else, for, while...) is never a good idea.

if (numHours >= 8) {

Semicolon simply terminates if and that is why else is unexpected, because else should be placed immediately after block of if.


For the above code result is showing only for the 10 hour sleep..Not for 5 and 8 hours.
What has to be done?


You simply need to use console.log statements:



don't put semicolon after if because the program terminates here and the execution of else doesn't occur


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.