12. Function if/else "...There was a problem with your syntax."


#1

// Write your function below.
// Don't forget to call your function!
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));
console.log(sleepCheck(5));
console.log(sleepCheck(8));

It's giving me the "Oops, try again. There was a problem with your syntax." Error. As far as i know, this is exactly what people have been doing to solve their problems but i can't figure out why it's not working.


12 Function if/else help
12 Function if/else help
#2

Here you have a real problem with the syntax.

var sleepCheck = (numHours); {

First of all you lack the fucntion keyword here and the other thing is the semicolon that should not be there.


#3

Stupid mistakes have been fixed, but still getting the same message.

// Write your function below.
// Don't forget to call your function!
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!";
}
};
console.log(sleepCheck(10));
console.log(sleepCheck(5));
console.log(sleepCheck(8));


#4

Your missing a { for the if so the } before else closes the function and not the if.


#5

I'm also having issues with this lesson. things are showing up in the results box correctly but the lesson says it's wrong. I've refreshed and restarted the browser since that's been an issue before but that's not the problem this time.
Here's the code I have. (Also is this the right way to ask for help on the same lesson?)

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!";
}
};
console.log(sleepCheck(10));
console.log(sleepCheck(5));
console.log(sleepCheck(8));

#6

Seems to be ok, what is the error message? If it mentions one of the two strings maybe replace this string with the one mentioned in the instruction or the error message as you probably have a typo in it.


#7

Thanks! I replaced the text I had with the one in the error message ("You're getting plenty of sleep! Maybe even too much!") and that worked. What's strange about it though is that I copied and pasted that same string from the instructions to begin with and that didn't work. also just typing it in the way the instructions said didn't work....But at least I can continue on now! thanks again.


#8

I am getting an error also, although my code works. The error 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.

My code is:

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

Any ideas on getting around the error would be great.


#9

I'm getting this error:

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

but the return is showing up on screen !!


#10

joomarcosxavierlavag,

I like your code. The only thing I changed was:

return "You're getting plenty of sleep! Maybe even too much!";}
else {
return "Get some more shut eye!";}
}
// After that change it worked for me. For the heck of it I also changed the following:

console.log(sleepCheck(10));
console.log(sleepCheck(5));
console.log(sleepCheck(8));


#12

for me, the adicional ) doesn't worked !


#13

so anything we can do about this?
skip this lession?


#14

The exercise is doable, it is just a bit picky about the output messages. So if you have a problem please open a new thread with a descriptive title something like: exercise number error message or problem and in the text post your code (well formatted) and give a short description what the problem is what question you have about it or why you expect it to behave differently.

Thank You!


#15

I have the same problem... this is my code

SyntaxError: Unexpected token else


#18

// Write your function below.
// Don't forget to call your function!
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!";
}
};
console.log(sleepCheck(20));
console.log(sleepCheck(4));
console.log(sleepCheck(40));

for the last }; before the console.logs...why do we have to have a 2nd curly bracket? I had only one before to close off the else statement.


#19

The last curly bracket closes the sleepCheck function:

var sleepCheck = function(numHours) {
  if (numHours >= 8) {
    return "You're getting plenty of sleep! Maybe even too much!";
  } //<--- Closes if statement
  else {
    return "Get some more shut eye!";
  } //<--- Closes else statement
}; //<--- Closes sleepCheck function

#20

This worked for me:

Spoiler Alert!

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


#21

I just had this problem too, but got it to work after changing the spacing in the if/else statement. Once I got the return "you're not getting..."; to fit on one line, I no longer got the error messages.


#22

Thanks so much, for some reason when I was getting desparate, I chucked in extra syntax like a semi colon in my first line!!!