Function and if/else (especially confused with else)


#1


Basically what happens is that I get "SyntaxError: Unexpected token else" in the output and the codeacademy prompt thing only says "Oops, try again. There was a problem with your syntax." Obviously this is related to the else part of the code and I encountered the same issue in the section 'Functions, return and if/else". Now, I did kind of "cheat" as to say, because I was quite sure it was right. However, it becomes clear that it is not a one-off and so it is definitely some error on my part. Could someone please explain this to me. Thx.


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

this line:

if (numHours >= 8);

the semi-colon your use here prevents your if/else clause from executing properly


#3

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