Functions & if / else (I think it works but Codecademy doesn't)


#1



Oops, try again. It looks like sleepCheck() isn't returning "You're getting plenty of sleep! Maybe even too much!" when numHours is 10. Check your if / else syntax and whether you're using the correct comparison operator.


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


#2

This should be in the Introduction to Functions in JS category.


#3

You have an extra spaces try removing it this lesson is expects you to have exactly the same text


#4

CodeCademy is very picky about the quotes. Make sure you copy it exactly as the lesson asks


#5

I'm having a problem on this one too.

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

sleepCheck (10);
sleepCheck (5);
sleepCheck (8);

I put in: else if (numHours < 8) but then it would only check the first sleepCheck.

That said, I dont think you need to use console.log at all in this one


#6

@codernatorgator

If you're having a problem, please post this in a new topic with your question + error message + code. Just a question of keeping the forum clean.


#7

@ginanennigergmail.co

Don't log this to the console! You should call it. To call a function remember this syntax:

functionName(optional parameters);

#8

This exercise was driving me nuts and I just figured it out. My program was generating all three responses, but Codecademy wouldn't let me pass the exercise. After a lot of trial and error, this is it:

Codecademy will only accept your code for this exercise if you only type 1 blank space after "sleep!" and before "Maybe." You cannot enter 2 spaces between the exclamation point and the "Maybe" because if you do, Codecademy won't accept it. It must be ONLY one blank space between exclamation point and the word "Maybe." Otherwise, you will get stuck.

This is a really lame bug to have in the program since all people are taught to put 2 spaces after punctuation!


#9

Thanks a lot !!!
This seriously worked!


#10