12. if/else


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/2/4?curriculum_id=506324b3a7dffd00020bf661


SyntaxError: Unexpected token {


// 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(numHours <8) {return  "Get some more shut eye!";}

}

  sleepCheck(10);  
  sleepCheck(5);  
  sleepCheck(8);


#2

else can't have a condition, since it is everything else.


#3

If a number isn't greater than or equal to 8, what's left? Just numbers that are less than 8, right? So you don't need to add the condition numHours < 8 in the else part.

In fact, you can't use give a condition to the else part, because like stetim94 said, else means anything that didn't meet the first condition.

There is a variation on the if/else condition called if/else if where you can specify a condition for both parts, but you can't do that with just if/else.

just if/else

if (condition) {
  //if the condition is met, this code runs
}
else {
  //if the above condition isn't met, this code runs 
}

if/else if

if (condition) {
  //if the condition is met, run this code
}
else if (another condition) {
  //if the first condition isn't met, but 'another condition' is met, run this code
}

Multiple Else If's

You can use more that one else if (as many as you need really) like this:

if (condition) {
  //if the condition is met, run this code
}
else if (another condition) {
  //if the first condition isn't met, but 'another condition' is met, run this code
}
else if (another another condition) {
  //if the first two conditions aren't met, but 'another another condition' is met, run this code
}

And you can even use a plain else at the end to capture any remaining scenarios that weren't covered by your conditions like this:

if (condition) {
  //if the condition is met, run this code
}
else if (another condition) {
  //if the first condition isn't met, but 'another condition' is met, run this code
}
else if (another another condition) {
  //if the first two conditions aren't met, but 'another another condition' is met, run this code
}
else {
  //if none of the above conditions are met, run this code
}

#4

sorry for the late reply havent been able to get on here. server problem. thanks for the help. i have taken out the condition of the else statement. its still coming up as the same error. its driving me mad!!!!


#5

thank you for the correction. its still coming up with the same error


#6

found my problem. thank you anyway. :slight_smile:


#7