12. Functions & if/else


#1


Exercise 12

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


#2

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


#3

return "Get some more shut eyes!";
instead of
return "Get some more shut eye!";

Error founded, sorry -.-


#4

i noticed it, thanks :slight_smile:


#5

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)


#6

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


#7

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


#8

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


#9

HI the else statement doesn't take any condition

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

#10

it returns "get some more shut eye!" :confused:


#11

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


#12

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.


#13

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


#14

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.


#15

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

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

#16

does the same apply for else as well?


#17

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


#18

finaly here

var sleepCheck(numHours){

it should be

var sleepCheck = function(numHours){

#19

awwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww whatt a stupid mistake hahahaha ty vry much


#20

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