My checkHours function executes an extra time without being called


#1

<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.>
12 Functions & if / else

<In what way does your code behave incorrectly? Include ALL error messages.>
The response from CodeAcademy is:
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.
But the function did run correctly and I had not used numHours = 10!

Here is the output from my code. I have added a log message to monitor the value of numHoursYou will see the correct response is seen for sleepCheck(5), but then the function has run again with numHours = 10!
start of function execution
5
Get some more shut eye!
start of function execution
10
You’re getting plenty of sleep! Maybe even too much!

```

Replace this line with your code.
Here is my code:
// Write your function below.
// Don’t forget to call your function!

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

sleepCheck(5)

<do not remove the three backticks above>

#2

@microsurfer47589

What is the exercise telling you to do?


#3

The exercise asks for the function “sleepCheck()” to be created and then called 3 times:
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.
I did this and sleepCheck() worked just fine, giving the 3 correct anwwers, but then the function ran an extra time, as if there was a fourth command of sleepCheck(10 ).!


#4

Hi try to change your console.log() by the return statement


#5

Thanks Wizmarco - I replaced “console.log” with “return” for each of the messages in the if/else block and the script ran correctly!