Function & if / else


#1

I'm having trouble with this part. Here is my code

var sleepCheck = function(numHours)

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

When I hit submit, i get the error code "SyntaxError: Unexpected token if"

Any help would be nice


#2

This should fix it! :slightly_smiling:


#3

I tried both of those and i still get the "SyntaxError: Unexpected token" .


#4

also after here you want a { instead of a ; as cade suggested

followed by another } before this one.


var sleepCheck = function(numHours){

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

You can also do console.log(sleepCheck(10)); to log the result to the console.


#5

I have no idea why mine doesn't work! Help please!

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


#6

It just says:
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.


#7

Hey Chipplayer17082... your code looks real nice! so we no there is no syntax error's. Did you try just calling out each sleepCheck value individually and clicking "save and submit"?

sleepCheck(10);
---Click save and submit---
sleepCheck(5);
---Click save and submit---
sleepCheck(8);
---Click save and submit---

All I can really think of is that the console is only reading the sleepCheck(8); in your code because it is the last value on the last line of code.
That's all that is making sense to me right now... give it a shot. Hope this works for you!


#8

I'm getting the same thing. It did this to me in a previous tutorial where I went "off script" and typed a different sentence to the one they asked me for (couldn't bring myself to be at a Bieber concert so changed the name) Once i typed it correctly it all worked. Not happening on this one though. checked and rechecked cant find whats wrong


#9

I just noticed chipplayer that you've type "to" instead of "too" maybe that will help?


#10

ok finally got it. I had an extra space between two words.... pffft.


#11

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

This my new code and now i get the "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." error


#12

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

Look for the difference.


#13

Thank you so much! I didn't realize that. big help


#14

Yeah, I tried doing them individually first but i saw someone else writing them like that so i thought that might work too. And I tried again but it doesn't work still, I'll just skip it then.


#15

Hey guys - i was also stuck but took a hard look at the semi colon and brackets and managed to correct it.

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