12. Functions & if/else

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

Exercise 12
<In what way does your code behave incorrectly? Include ALL error messages.>

Can someone explain me what’s wrong here.
It prints what the exercise aks and still says that it doesn’t return “Get some more shut eye!” when numHours is 5.

```

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

<do not remove the three backticks above>
5 Likes

Hi your else statement should return…

return "Get some more shut eye!";

insteaf of

return "Get some more shut eyes!";

remove the s to eye

2 Likes

return “Get some more shut eyes!”;
instead of
return “Get some more shut eye!”;

Error founded, sorry -.-

i noticed it, thanks :slight_smile:

1 Like

Hi!! I’m having similar issues, but my error message says “SyntaxError: Unexpected keyword ‘if’. Expected an opening ‘{’ at the start of a function body.”

Please, let me know if you can identify any errors in my coding:

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 some more shut eye!”;
};

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

Hi You didn’t open the function sleepCheck It should be like that

var sleepCheck = function(numHours) { #open the sleepCheck function 

#here put your if and else statement 

}#Close the sleepCheck function 

to your if and else statement remove the closing } of the if statement

if(number of hours of sleep >= 8) 
{
return "You're getting plenty of sleep! Maybe even too much!"; 
}; <-- this one 
else
{
return "Get some more shut eye!";
};

Then this Line


if(number of hours of sleep >= 8)

You should remove number of hours of sleep and put the argument numHours

2 Likes

Thanks so much for your reply!! It works!!

Hi, i’m getting the same error
below is my code :frowning:
var sleepCheck(numHours){
if(numHours>=8){
return “You’re getting plenty of sleep!Maybe even too much!”;
}
else if(numHours<8){
return “Get some more shut eye!”;
}
};
console.log(sleepCheck(10));

1 Like

HI the else statement doesn’t take any condition

if (condition) {
    #do something
}
else {
    #do something
}
2 Likes

it returns “get some more shut eye!” :confused:

and i 've use else if and it can follow a condition

var sleepCheck(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));
this is the code as u mentioned,that else shouldnt take a condition but still it gives syntax error.

And here you shoudl return

return "You're getting plenty of sleep! Maybe even too much!"; 

instead of

return "You're getting plenty of sleep!Maybe even too much!";

you should have the space between ! and Maybe

1 Like

var sleepCheck(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));
gave the space before ‘!’ still error.

here remove the ; after the closing } of the if statement

if(numHours>=8){
return "You're getting plenty of sleep! Maybe even too much!";
}; <--
1 Like

does the same apply for else as well?

:’( still that error

var sleepCheck(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));

finaly here

var sleepCheck(numHours){

it should be

var sleepCheck = function(numHours){
1 Like

awwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww whatt a stupid mistake hahahaha ty vry much

2 Likes

This is my code. I can’t find any issues, but it says when I run: “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 are 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));