12) My code seems accurate. However several error messages


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

But i am faced with several messages:

Unexpected keyword 'if'
Unexpected keyword 'else'


#2

this is the correct syntax for creating variables as functions

var functionName = function(argumentsWhichAreOptional){//opening brace
                             //code or statements

                   };//closing brace

#3

you are overusing ; . One of the main functions of the semicolons is to tell the js interpreter stop right here (mod please correct me if I am wrong).


#4

yep thats right but javascript unlike other languages like java is not really strict on semicolons


#5

I'm not sure about the original poster, but I know the problem I'm having is figuring out how the braces for the function interact with the braces for the if/else statement. So, if you see the code I wrote below, as I understand it there should be an opening brace "{" after delcaring the function- this brace wouldn't be closed until we were done with our new code block. BUT, the if/else statement would need a set of braces all of its very own! Depending on which way I've tried to set these brackets I've gotten either "Unexpected token" or "Unexpected else" or, the way I originally wrote it as you see below, "Unexpected end of input".

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

#6

somewhere there is a rule "the curly bracket you open first is the last one you close".

Proper indentation is a key here, to spotting err in syntax.

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

I indented your code and added the closing curly bracket


#7

Ahh, there we go. I've made so many basic mistakes since yesterday that I assumed the problem was in the main block of code. Thank you!

Also, sorry about the lack of indentation. It was there in my code editor but didn't translate with the copy + paste. I should've reformatted.


#8

Thanks so much. That really helped me!