12/13 Syntax error


#1

Whats wrong with my code?

// 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!";
    };
sleepCheck(10);

Error: SyntaxError: Expected '{'


#2

You forgot the {} around your function and you need to get rid of the semicolons after your conditional statements. Here is an explanation why:


#3

Hi, I get the same error with the following code:

var sleepCheck = function sleepCheck(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);

What am I missing?


#4

Don't define the function's name twice. You have to options:

  • function sleepCheck (numHours) { ... };
  • var sleepCheck = function (numHours) { ... };

#5

the systems says the function doesn't return the "Get more shut eye" because of my if/else syntax and the comparison?


#6

I am having a similar problem, what is wrong?

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


#7

You closed the function after the if's "}". That first one closes the if, the other closes the function. Include the else inside the function. Something like this.

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


#8

Try adding the semi-colons after the return statement. Like this

...
return "You're getting plenty of sleep! Maybe even too much!";
...
return "Get some more shut eye !";
...

If it doesn't work, could you tell me if the console shows an error or it displays the information that it should?


#9

Ok! I made some adjustments and it still isn't going through. How does this look?

var sleepCheck=function(numHours){
if (number of hours of sleep>=8) {
return "You're getting plenty of sleep! Maybe even too much!"
};

else {
return "Get more shut eye!";
};
};
sleepCheck(10);
sleepCheck(5);
sleepCheck(8);

#10

// 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!";
}
};
sleepCheck(8);

I'm kind of stumped on this one as well. The code works, but I want to make sure it's set up the correct way. Any feedback is appreciated.


#11

I believe you're missing a semicolon after 'return "You're getting plenty of sleep! Maybe even too much!"'.


#12

I added in a semicolon in that spot; the "else" statement has one, so it seems the "if" statement should have one, too. No luck! I know it must be a simple solution, I need to run it through another browser and see if it goes through. Thank you!


#13

Ok. I thought it needed one after because in the hint section, it says you need a semicolon after each line within the braces.
Yeah I feel the same way. I posted my code, it runs but I'm not sure if it's right.


#14

I ran it through another server and this time I am getting "unexpected '(' ". Truly stumped. Other than that, I have been adding semicolons in and out and parentheses in and out. I notice some people have their return statements in parentheses and are able to run the code successfully.


#15

Ok I found it, I believe. I didn't actually use "numHours" as a function. Correcting that certainly helped, but now also I can see "You're getting plenty of sleep..." printed out for 10 hours, it isn't processing 5 hours nor 8 hours. Hmm.


#16

I saw that but forgot to bring it up when I was replying. Have you tried the code I posted? It works but it doesn't process the other two number as you've said. Unless I change the first sleepCheck(), then it'll output different.


#17

Same thing! It will process 5 hours of sleep and give me the correct response, but then I get an error stating that I did not attempt to process 5 hours of sleep, basically. Even though it prints out correctly! It does this in two different browsers. I would love to get my badge because the code is running to a point, but I am going to move on, for now. Rock, Paper Scissors here I come...


#18

haha same thing since it worked I kinda just moved on. I'll just patiently wait for someone to correct my mistake. Maybe I'll go back at it tonight and try it. Good luck.


#19

Someone in another 12/13 thread pointed out that the computer is only running the last code for 8 hours, but not the first two. I needed to write console.log(sleepCheck(10)); and then console.log(sleepCheck(5)); and then console.log(sleepCheck(8)); to have the computer run all three. It does successfully, but tells me that there is still an error.

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.


#20

Are you referring to when sleepCheck is set to 5, it doesn't return a "Get some more shut eye"? If you are then it works fine for me. It prints and lets me proceed to the next lesson.