Lesson 12 if/else statement Need Help


#1

Need Help, Getting syntax error unexpected token else

Below is the link to the exact excercise


SyntaxError: Unexpected token else


Replace this line with your code. 

// Write your function below. 
// Don't forget to call your function!
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

You don't use a { to open your if statement, which you need to do. Additionally, you need to make sure that your spelling is exact because the SCT is case sensitive, so

should have a capital 'Y' I believe.

Edit: I didn't say this because I figured it was obvious, but I will go ahead and clear up any future confusion, in this line:

you need to get rid of the semi colon AND add the opening bracket, {.


#3

Problem is here:

var sleepCheck = function (numHours){
    if (numHours >= 8) { // should be without ; and with {
     return "you're getting plenty of sleep! Maybe even too much!";
   } else {
     return "Get some more shut eye!";
  }
}; // missed }
sleepCheck(10);
sleepCheck(5);
sleepCheck(8);

#4

Thank you so much all :smile:


#5

FYI I believe they opened the bracket after (numHours) so I don't think that's the issue while I ran my successful script without capitalizing the "you're" and it didn't have any issue running it.


#6

actually that is an issue, because it's an if statement. you need to open if statements with brackets. :))

you ran this in the lesson and passed the test? That's good then! Normally the output is just extremely case sensitive, so I always suggest to copy and paste the message that the directions ask you to print. Anyways, I'm pretty sure that they got everything worked out, but thanks, :))


#7

So here's what I have that was accepted -

var sleepCheck = function (numHours) { // <- you'll see here that the
//bracket is opened and the if statement and else statements are included
//below within the script
sleepCheck(10);
sleepCheck(5);
sleepCheck(8);
if (sleepCheck >= 8)
return("you're getting plenty of sleep! Maybe even too much!");
else
return("Get some more shut eye!");
}

While it appears javascript is a case sensitive language, capitalization shouldn't matter when you're establishing an output string? I'm still learning however so I could be mistaken.


#8

though this is true, this isn't the reason that you may have trouble with certain strings sometimes. In general, javascript/an editor doesn't care what you print, but the lesson's correctness test does if that makes sense. Though you typed something almost identical, there correctness scan is something that looks specifically for an output, so if it doesn't get exactly what it's looking for, then you'll get an error message.


#9

thank you a lot:slight_smile:


#10

I got something similar but i dont understand what i did wrong:

var sleepCheck= function(numHours)
{
if("numHours">=8)
{
return "You're getting plentu of sleep! Maybe even too much!";
}

else
{
return "Get some more shut eye!";
}
};
sleepCheck(10);
sleepCheck(5);
sleepCheck(8);


#11

There are two mistakes, 1st one in your if statement :

\By adding quotation marks to numHours your converting the parameter (numHours) into a string text, therefore causing the condition to be invalid, You gotta remove those quotation marks on Numhours so the parameter would be valid for the condition to check

2nd Mistake: theres a typo in your return statement

Hope it helps :slight_smile:


#12

Keep the parenthesis.
Remove the quotations that turn (numHours) to a string.


#14

Thanks !! This really helped


#15

I also have a problem what am I doing wrong? any help here? ill post it below

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

#16

#17

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