Another exercise 12 functions if else problem


#1

Hey guys, newbie to coding here. I'm having an enormous problem. This exercise refuses to let me pass. Like a lot of people here, I'm stuck on exercise 12 functions & if/else. Here's 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 shut eye!";
}
sleepCheck(10);
sleepCheck(5);
sleepCheck(8);

When I try to run it, it tells me that return is not in the function, which really boggles my mind. I've also tried to replace the returns with console.logs and when I do that, it tells me that numHours isn't defined.

I swear this exercise must've been crafted by Lucifer himself. Please help, and I'll love you dearly for it.

Edit: only part of my code was being shown in a box, so I put it closer together, probably didn't fix it tho'.


#2

Hi @xaches,

you accidentally close the function too early with the }

Notice the } under var sleepCheck? You close the function without any code in it. Thus, there will be no code running inside the function and no return involved.

The rest of your code is correct.

Just deleted the } I mentioned, and add that } in the last line to close the whole function with the if/else statement inside the function.

Then, it should be fine. :slight_smile:


On side note, { } is very important, remember to go through the placement every time when writing your code. Few other users facing similar situation with { } placement and the code is not running.

If you'd like to read more and learn to check the placement (try see if you can spot it), here is one similar to yours: (close function too early)

Another one: (close else if too early)


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.