Lesson 12! Functions and if/else


#1

Hey you all! I am at my end staring at my piece of code to get it to say "You're getting plenty of sleep! Maybe even too much!" for the equal to or greater than statements. When I click submit this is what pops up...

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.

Can anyone spot where I've gone wrong. Thanks so much!

strong text

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

#3

I changed mine to sleepCheck(x) and was getting that error msg as well. I did insert a semicolon after the final closing bracket....};
I believe this is correct . I will follow your post!


#5

thanks so much Ill give that a shot when I get back to it.... cheers.


#7

As far as I can see you're missing a } to close your function. The semicolon after it would be correct but is probably not the reason for an error message like this.


#8

Hey there, I did try this but am still having errors block me from moving forward. Thanks though


#9

I put my closing } just above my console.log statements... Its kind of funny, because in the console when I click submit I get :

undefined
Get some more shut eye!
undefined

Which puzzles me because one of the three works, and as far as I can see I did both "if/else" statements the same.


#10

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

like this :smiley:


#11

Thank you! This worked! Well, kinda.... I put my numHours >= 8 in parentheses which was the only change I could see to make..... With no results... Finally to see if it were a bug I copied/pasted your code in and alas, it gave me the green light. I think I'm going insane, but at least forward! Thanks for your help, much appreciated...


#12

Could you post your updated code on that again (that one that gave you the undefined output)? As far as I know this exercise is pretty picky about the output message but not bugged so far but if you found one it would be good to locate it.


#13

your welcome :smiley: