12 Functions & if/else error


#1

I cannot work out out why I'm getting the error "SyntaxError: Unexpected token else"

My code is:
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));

Hopefully this screenshot works too! Any help is greatly appreciated!


#2

@anyaj_wlg When you call your function, don't console.log it. Just call it like so:

sleepCheck(10);

#3

Tried that (based on another thread) and I'm still getting the error with and without the console.log part :frowning:


#4

@anyaj_wlg I see the problem! You never closed your function. Add another } at the end of the function.


#5

I am completely blind!!! So I have the {} on lines 4,7,9,11 - where do I need another?


#6

Line 5, after the if statement, and one on line 11 after the else statement :slightly_smiling:


#7

@anyaj_wlg Your code should look like:

var sleepCheck = function(numHours) { 
  if (numHours >= 8) {
    do this
  } else {
    do this
  }
}

#8

AH HA!!!! OMG Thank you! I was going insane trying to work out the problem!

So I was trying to to open and close for each statement, and not including them around the entire code... Gotcha :grinning:


#9

Someone please help me.
This is my code, where am I going wrong?

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


#10

@ladylee1 Your code should look like

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

Right now, you have a semicolon right after the if statement, which is messing it up:

var sleepCheck = function(numHours) {
 if ( numHours >= 8); /*left of this comment is your error*/ {
 return "You're getting plenty of sleep! Maybe even too much!";
 }
 else {
 return "Get some more shut eye!";
 }
 /* missing curly bracket */
 console.log(sleepCheck(10));
 console.log(sleepCheck(5));
 console.log(sleepCheck(8));

#11

you missed } at the end but apart from that it works so thank you for that! I appreciate your help!


#12

Oops! Missed that! Looks like I learned something, too :slightly_smiling: No problem! Glad I could help.