12. Functions & if / else


#1

// Write your function below. 
// Don't forget to call your function!

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

 else {
    return "Get some more shut eye!";
}

Why do i get syntax error?


#2

Hi @boardblaster29194,

Here, remove the semicolons after the two places and add a { bracket after the if (condition)

  if (sleepCheck >= 8);  //<--- remove this semicolon and you forgot a opening { bracket
  return "You're getting plenty of sleep! Maybe even too much!";
};   //<--- remove this semicolon

Tell me if you have any other error showing :slight_smile:


#3

Thank you :smiley: Got it to work!


#4

Please, why is this error showing?


#6

Yes, the first parenthesis for the console.log and the second for the function parameter


#7

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

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

This worked for me


#9

Hi @boardblaster29194,

this code is wrong:

else { (numHours <= 8);
return "Get some more shut eye!";
}

The if/else syntax can be seen like this:

if (condition) {
    // if condition is true
    // do this code
} else {
    // do this code instead
}

Now compare the above syntax to your code, see if you have added an unnecessary condition).

You only need to state what is the if condition, the rest that do not fall into the if condition, will fall into else.


A few users have been struggling with else part, two examples like your situation can be seen here:


#10

You need to call the function @aspyre1234, not console.log

The exercise says:

Then call the function with different hours of sleep

Call the function with 10 hours of sleep, like this: sleepCheck(10);
Call the function with 5 hours of sleep.
Call the function with 8 hours of sleep.

Run the function with

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

#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.