Functions & If/Else - sleepCheck - HELP, please!


#1

I am working on the sleepCheck lesson and the code works. It prints to console the three examples that the lesson asks to be printed out. But I keep getting an error message that sleepCheck(10) doesn't work. I cannot move on. I followed the instructions in someone else's post, to close the browser, reopen, and copy the code. I continually get the same error message. I even tried another browser. Below is the code and the error message. Anyone? Anyone? Thanks, I appreciate the help! Cheryl

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

Yes you're code seems to be working. I can pass using it. Did refreshing the page really not help? Just give it another try, if this doesn't help maybe it might be a real bug.


#3

Hey thank you! I just refreshed the page again, twice. It is still giving me the same error message. I happen to be using Chrome. But I tried in Mozilla, too, just to try. It gives me the same error message.


#4

As said I can pass without any error message using firefox. I'll keep an eye on it. But as said the code is ok so maybe just go on and do this one later.


#5

Try this
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);
Good luck :smile:


#6

how do you call a function!!!! (:rage:


#8

You don't need console.log(sleepCheck(10)); since your already returning the value in the function. Just use
sleepCheck(10);


#9

If you are interested in seeing the results of your function calls then console.log() is the way to do it.


#10

You actually need console.log because return does not output to the console but to the calling scope. So if you don't use console.log you'll only see the last result cause of a console feature echoing the last unused value.

@mmoria by using the function name and appending () and if a parameter is used by using a value in the () e.g.

sleepCheck(20);

Also how is this related to the topic and can you ask a bit more politely?


#11

Thanks! I tried it. It doesn't work. It gives me a response for the 10 hours but it still gives me the error message that it has a problem with 10. I tried adding all three variables and it doesn't pick them up. So I went back to the console.log version and it prints all three to the console while still giving me the error message.


#12

Thanks, again. I tried everything that the posters suggested again this morning and still it doesn't work. It keeps giving me an error message for the number 10. I even tried a different number. I figured out how to move on so I will come back to it. Thanks, again!!!


#13

A post was split to a new topic: Quarter()


#14

As the code seems to be ok I'd guess it is about a typo in the output message. One guess would be the additional space between ! and Maybe but anyway rather copy and paste this sentence from the instruction, the important part of this exercise is ok.


#15

Have you tried running the same code from a different computer if you can? What Operating System are you using too?


#16

I'm confused... I ran the following code successfully, but it should've returned the "plenty of sleep" text, however, it returned the "get some shuteye" text... here's the code I ran:

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


#17

You aren't returning either message ...

All of your decision making is not part of your sleepCheck function at all.

I guess the code checker had a melt down seeing that and just gave you the pass. :smile:


#18

So what then? Should I be removing the return code on line 3 and entering a command after my if/else code at the end?


#19
  • Remove return numHours(10);, because, well, what does that really mean anyway?
  • Put all of your if/else decision making inside of your function instead of after it.
  • Instead of console logging those message strings, return them.

#20

I've been playing with this code, but I'm still not seeing it. Can I have an example of a successful code? I'm obviously not writing this correctly...


#21

The code in the first post in this thread is correct except for the extra space before "Maybe".