12. Functions and if/else


#1


// 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!";
}
};
It say that this is an error-->else {
return "Get some more shut eye!";
}

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


#2

Because your else is outside your sleepCheck() function :slight_smile:
Here's the basic outline of your code:

var functionname = function() {
  if (...) {
    ...
  }
}; //This is the close of the function
  else {
   ...
  }

It should look like:

var functionname = function() {
  if (...) {
    ...
  }
  else {
   ...
  } //Else is now in the function
}; //This is the close of the function

The examples were just to show the problem, don't copy paste this code :slight_smile:


#4

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

//Its only returning the first sleepCheck(10) ????


#5

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