12. Functions if/else problems


#1

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

What am I doing wrong here. This looks like it should be working but I'm obviously missing something. Thanks for the help


#2

Hey @devrunner48568,

You're right. :wink:

The problem is this. Look at it closely.


#3

Got it! thanks haha. Can't believe I missed a parenthesis!


#4

Also no semicolons after you if case:


#5

I've stucked here too.
Even if I copy / paste this code:

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

I get SyntaxError: Unexpected end of input.

Why?

Thanks for the help


#6

Seems to be a missing } to close the function.


#7

of course... thanks!


#8

@ajaxrockstar04812 Forgot ';' at the end of }(x2)
and for got }by its self


#9

@ragezapper
// 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));
what is wrong with this?????? :triumph: I got a syntax error say in syntax error


#10

Well no semicolons at the end of conditions, see this link as an explanation:


#11

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

This is exactly what I did, and the result was correct, and it appeared on the screen and everything, but I can't move forward because I get "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." Please help!


#12

Still no semicolon after else but this is probably not the error. The ' in "You're" seems to be incorrect apostrophe instead of backtick. So as the syntax is ok copy and paste the messages from the instructions.


#13

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

I'm sure this is correct1


#14

Sry it is not the semicolons are a problem:


#15

it already been done


#16

So why did you post a code with it still in it? Also "eventoo" probably needs a space in between. What happens is probably that the expected output is checked against you're output so they should match 100%.


#17

help me

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

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

and heres what i get

You're getting plenty of sleep! Maybe even too much!
Get some more shuteye!
You're getting plenty of sleep! Maybe even too much!

but
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.


#18

Hi look what the error says

Oops, try again. It looks like your function doesn't return "Get some more shut eye!"

return "Get some more shuteye!";

Put a space between shut and eye!

return "Get some more shut eye!"

#19

Hi everyone,

Why does the console show only one response when I called the sleepCheck function 3 times? Shouldn't it show three responses in the order of the calling?

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

Instead I see one response, "You're getting plenty of sleep! Maybe even too much!"

Shouldn't I be getting:
"You're getting plenty of sleep! Maybe even too much!"
"Get some more shut eye!"
"You're getting plenty of sleep! Maybe even too much!"


#20

Hi try to use the console.log on your calling function sleepCheck() like that

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