12. Functions & if/else



Code passes but I'm not clear on why it's working the way it does.

The response for one of the three numHour responses is being presented in the console log.

I'm providing three separate numHour responses, so why is it that the console log only has the results of one of the three sleepCheck calls? Shouldn't I be getting the if result twice and the else result once?

If I add in lines to the bottom to add in
Then it will start to print each line in the console log individually.

What exactly is running a variable multiple times doing and if only the last version is being printed in the console, why would I ever do it multiple times like in this exercise?

I sure would prefer more real world type examples of coding (even like the wrist slash inducing Bieber game).

Replace this line with your code. 

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


Just because to show some people how the code works. It's just experimenting with the code.
This console is a custom console by codecademy. Sometimes it does strange things but it's necessary to work that way. Don't bother. Just code and proceed to next lessons :wink:


At least yours passed! I've ran this exact code multiple times, and it keeps giving me the one line in the console and saying "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." I'm so confused and frustrated! If I try to use the console.log when calling the function, it prints 4 lines in the console and gives me the same error.

I actually just copied-and-pasted the code you posted above (after painstakingly comparing the two) to see if I could "trick" the thing, and that allowed me to pass, while also producing the one line. I've run into a lot of very frustrating bugs and other obstacles that prevent the correct answer from displaying how it should and being accepted... Glad to know I wasn't the only one who had trouble with this one!


Same problem with me. Felt glad to see that the problem is solved by this method. i mean by copying the lines verbatim.

"You're getting plenty of sleep! Maybe even too much!";
"Get some more shut eye!";

I too copied the two lines given in the instruction part and pasted in the program and found that I got passed.
Thanks, moving ahead!


sorry to hear that!

For a mental exercise, could you print your original code here (if you still have it handy).

One of the more valuable things I'm finding about this code academy experience is debugging. It's hard to do because we don't know all of the rules yet. But, looking for a missed semi colon or parenthesis, or whatever else has been valuable for me.


Had the same problem. Thanks for the solution.


If by the "original," you mean the code I typed prior to copying-and-pasting, I don't have it anymore... I was very thorough with my debugging though, so I am 99.9% sure they were exactly the same. I definitely could be wrong, but there was another question I ran into on this site where the solution was something very similar, meaning that your code could be correct, but you had to do something weird (like copy-and-paste something in) to make it work for some reason! If I had the original, I would gladly let you try to debug it, though-- I appreciate the reply! lol


Could somebody tell me what's wrong with mine?

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


Example function declaration

var functionName= function(parameter){
code code code;

See the difference? Add 'function' before your numHours parameter


Had just same issue. Very annoying ;S !!


Yeah, I've noticed after painfully comparing the code from OP with mine, I
felt like such a dumbass...


Nah man, we're all rookies at javascript :slight_smile:

Just keep at it.


Hey guys

Coudn't give you an exact explanation as to why but this code juste made it work for me. I think it has something to do with the "return" and printing the function:

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

var tenHours = sleepCheck(10);
var fiveHours = sleepCheck(5);
var eightHours = sleepCheck(8);

If anyone would have a more exact explanation, thank you!


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