Functions & if / else


#1


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!";
}
};
sleepCheck(10);
sleepCheck(5);
sleepCheck(8);


#2

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.


#3

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


#4

You simply need to use console.log statements:

console.log(sleepCheck(10));
console.log(sleepCheck(5));
console.log(sleepCheck(8));

#5

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


#6

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