12/13 Functions & if/else


#1

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


#2

Need help, don't know why is isn't accepting this?


#3

I notice you have an extra bracket, and you don't need the parenthetical around your return phrases. Lastly, I would throw a semicolon on the end of the entire function (after your last bracket).

Hope that helps you!


#4

Same for me, in two dfferent browsers. Syntax error...

var sleepCheck=function(numHours){
if (number of hours of sleep>=8) {
return ("You're getting plenty of sleep! Maybe even too much!");
};

else {
return ("Get more shut eye!");
};
};

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

#5

if (number of hours of sleep>=8) {

this part isn't correct, there are no variables declared as number of hours of sleep
you might want to look at numHours


#6

Yes! I discovered I wasn't actually using the function :stuck_out_tongue: It runs through 10 hours of sleep but won't process 5 nor 8 now...


#7

it doesn't run through all of them because you're calling the function AND its returning properly, but it doesn't know what else to do with the returned value

when this is the case, it will only return the last function called.
sleepCheck(8); would return "You're getting plenty of sleep! Maybe even too much!"

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

if you did this, it would print out each result to the console.


#8

Let me try this then! I was calling it by just writing "sleepCheck". I am so glad you pointed this out; I would have never known. Thank you for your help!


#9

Ok, I can see that they all went through; 3 different statements that correlate properly. I am still getting an error though. This is what it says:

Oops, try again. It looks like your function doesn't return "Get some more shut eye!" when numHours is 5. Check your if / else syntax and whether you're using the correct comparison operator.

Any other ideas?


#10

there should be no semicolon ( ; ) before your else statement


#11

Hm, anything else? I removed it, but I am still getting the same message :frowning:


#12

Im wondering if this syntax issue has anything to do with the global vs local variable section we learned....anyone agree?


#13

Apparently there is a problem with my syntax

// Write your function below.
// Don't forget to call your function!
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);


#14

oh, its the {} brackets.. you should have 3 pairs of them
one for the sleepCheck = function another for the if (numHours >= 8) and else


#15

Now its saying

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.

// Write your function below.
// Don't forget to call your function!
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));


#16

strange, when i copied and pasted your code, it worked for me, try removing everything from your code, and pasting just what you had in your post.

just a small minor thing, though, the semicolon, it should go on the last bracket, the 2nd last bracket isn't needed.


#17

Just tried my code on internet explorer and it worked...I was using the 'chrome' browser

Thank you


#18

I have had similar issues with my safari browser, I come on here and have an exact match to what someone else types and it doesn't work.. It makes it very frustrating when I can't tell if I'm doing something incorrectly, or if my browser is just causing it to fail.


#19

i think its because the site is having some troubles lately, i noticed this afternoon some of the lessons didn't load at all, giving me an error 500 Bad Request


#20

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

};

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

won't work! I'm using chrome