12. Functions if /else


#1



Oops, try again. There was a problem with your syntax.


// 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 to much!";
}
    else {
        (sleepcheck <= 8);
    return "Get some more shut eye!"; 
}
};
sleepCheck(10);
sleepCheck(5);
sleepCheck(8);


#2

Your if statement needs to have an opening bracket but no semi colon,

example,

if (condition) {

also, an else statement cannot have a condition nested inside it. :grin:


#3

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

@bandit also has a valid point


#4

I dont follow..
"also, an else statement cannot have a condition nested inside it."
What do you mean?

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

#5

here, it clearly shows if has a condition. else can't have a condition, since it is everything else (all other possible cases)

after the (condition) of if there should be a opening curly bracket ({) to mark the start of the if statement, not a semi-colon (;)

you seem to have that fixed :slight_smile:


#6

But it still gives me error. Here is the code at it's whole.

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

Still getting > Oops, try again. There was a problem with your syntax.


#7

well here:

sleepcheck <= 8);

we can clearly see else still has a condition, it can't have (as you learned)


#8

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

Now im getting this error instead; SyntaxError: Unexpected token if


#9

else and your function still require the use of curly brackets ({}) , why did you remove them?

Please take a look at documentation / earlier exercise to see the syntax for function, if and else


#10

Thank you will do.. I think I'm on something here.

Oops, try again. It looks like sleepCheck() isn't returning "You're getting plenty of sleep! Maybe even too much!" when numHours is 10. Check your if / else syntax and whether you're using the correct comparison operator.

Now the correct answear shown but the lesson still not passed.


#11

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

How come it works when I've sleepCheck inside and not outside the curly brackets? Now giving me no result on the right panel but the exercise is passed hm.


#13

Why have you included your function calls inside your function?

Place them outside of your last closing bracket and it should work. :slight_smile:


#15

I entered the if/else conditional statement correctly without calling for a specific numHours slept, and i was able to pass this section.

Kinda let down... was hoping for a stricter code supervision.

hoping a moderator/administrator sees this~


#17

HI,
I an also stuck on this. Can anyone please help? I've read this thread over and over. Don't see what is wrong with my code. Thanks....

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


#18

look at this function:

var example = function(){
   console.log("awesome function");
}

the function uses {} to mark the start and end of your function, they are missing in your code


#19

like this? Still doesn't work.

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


#20

i was hoping you would figure out what you needed to nest inside your function.

if (numHours >= 8)

why did you remove the { from this line?

and then add a } at the appropriate place to mark the end of the function


#21

Thank you very much.
My head hurts!!!
I'm going to go through this lesson again.

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

#22

#23

please read the guidelines, if you need more help make your own topic.