1

Sorry if that title was unclear, let me explain.

I'm teaching myself JavaScript using Codecademy.com

I'm at a point where I'm learning functions using if / else statements and using returns to produce a result instead of console.log like I'm used to.

Anyways, the code I'm writing at this point in my training is...

var sleepCheck = function(numHours) 
{
    if (sleepCheck >= 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);

So I don't know what I'm doing wrong. The code returns a red error message saying

"SyntaxError: Unexpected token 'else'"

So I know it's, obviously, at the "else" part but I'm not sure how.

I came across this website so I need some help. If you can it would be much appreciated. Thank you!

5 Answers 5

1

jsFiddle Demo

Change your JavaScript to the following:

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

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

Description:

There were multiple issues with syntax, using a ; when not at the end of a line of code but rather the end of an if declaration. Also there was some issue with the if and else logic. Also inside the function you should be using numHours as that's the parameter of the function while sleepCheck is the function itself and won't make sense to use.

Note: I used console.log on the 3 function call to make debugging easier. Feel free to call the function any way you want.

Detailed Errors

//This line is perfect
var sleepCheck = function(numHours) 
{
    //This line shouldn't end in a ; as it is invalid syntax you are opening an if also should use the function parameter of numHours not sleepCheck
    if (sleepCheck >= 8);
    //this line isn't reached because of the above error there isn't an error here
    return "You're getting plenty of sleep! Maybe even too much";
//This also doesn't need to have a ; and shouldn't be closing the } as you are ending the function here
}; 
//This is now ignored because of the above but is good, doesn't line up with the above if because of the }; which should just be a }
else 
{
    //All good
    return "Get some more shut eye!";
}

sleepCheck(10);
sleepCheck(5);
sleepCheck(8);
Sign up to request clarification or add additional context in comments.

1 Comment

Remind me never to bother responding to questions while using an ipad :)
0

The semi-colon at the end of the if statement is declaring that the statement is done. Get rid of it (the one on line 5) and it should work.

1 Comment

Still does not work unfortunately. Now it says, "Expected an identifier, instead saw else."
0

I used 'else if' because in the instructions it said Otherwise (else) if the number of hours of sleep is less than 8, have the computer return "Get some more shut eye!"; So I assumed I needed to. It worked though. shrugs

    var sleepCheck = function (numHours) {
    if (numHours >= 8)  {
        return "You're getting plenty of sleep! Maybe even too much!";
    }
    else if (numHours < 8) {
        return "Get some more shut eye!";
    }
};

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

1 Comment

else in this situation does the same thing unless numHours is not a number and thereby isn't able to greater than equal or less than so it wouldn't do anything in those situations
0

You need to remove the semicolon after the if statement: if (sleepCheck >= 8); <---

Here's a useful rule to keep in mind:

In JavaScript never place a semicolon just before an opening curly brace or an opening parentheses because it will make the parser jump over the code block that follows.

Comments

0

None of the above solutions worked for me. Here is one that really works:\

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

On lines 3 and 5, delete the semicolon. They are causing your else syntax error. Make sure you include a semicolon on line 9 to end the function as per the instructions in the Code Academy lesson.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.