Lesson 12 Functions & if / else code help


#1

What am I doing wrong with my code??

This is the task:
Write a function named sleepCheck that takes the parameter numHours
Inside the function, write an if statement where if the number of hours of sleep is greater than or equal to 8, the computer will return "You're getting plenty of sleep! Maybe even too much!";.
Otherwise (else) if the number of hours of sleep is less than 8, have the computer return "Get some more shut eye!";
Then call the function with different hours of sleep
Call the function with 10 hours of sleep, like this: sleepCheck(10);
Call the function with 5 hours of sleep.
Call the function with 8 hours of sleep.

This is my code:

var sleepCheck = function (numHours)
if (numHours >= 8) {
return "You're getting plenty of sleep! Maybe even too much!";
}
else {
return "Get some more sleep!";
}
sleepCheck(10)
sleepCheck(5)
sleepCheck(8)


#2

Hi your if And else statement should be inside The function sleepCheck

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

#here put your if And else statement 

}# Close The sleepCheck function

#3

Hi :slight_smile:
Did you mean this?
var sleepCheck = function (numHours) {
if (numHours >= 8)
return "You're getting plenty of sleep! Maybe even too much!";
else
return "Get some more sleep!";
}
sleepCheck(10)
sleepCheck(5)
sleepCheck(8)

but when I do it this way then I get the following message: 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.


#4

Now You got another error... You should return

return "Get some more shut eye!"

Intead of

return "Get some more sleep!";

#5

Hey wizmarco :smiley: thanks! I got it.. must be sleepy myself since I changed shut eye to sleep without even noticing :stuck_out_tongue:


#6

it is not working for me..