12. Functions & if / else


#1

Hello, What is wrong with my code below?

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


#2

Remove the semi-colon.


#3

Like this? (doesn't work so I'm 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));


#4

you forgot to open curly brace at the end of the if statement


#5

Hmmm. I'm still not right:

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


#6

and tehn you will need to close it at the end


#7

you need curly braces on both the function and the if statement.


#8

OK, I'm a little confused. I though this was (<==) the closed c-brackets?

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


#9

so with an if else statement you close the if statement and open the else if or else statement right after. like this.
if (numHours>=8){ //this is where you open it
return "your text here";
}else if(){

}
but since its inside the function you have to have the if statements inside of the functions curly braces.
so
var sleepCheck = function(numHours){ // the curly brace is part of function syntax
if (numHours>=8){
return "You're getting plenty of sleep! Maybe even too much!";
}
else
{
return "Get some more shut eye!";
}; //<------- this will close your else statement but your function is still open


#10

that probably confused you more then anything. basically you need to open curly braces at the end of your function and close them after the if/else statement. and only put a semicolon on the last close curly brace"}"


#11

Still not working.

var sleepCheck = function(numHours){ // <---Where or does this c-bracket need to cclose?
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));


#12

return "Get some more shut eye!";
} <=== this will close the if statement
}; <==== this will close the function


#13

Yippeee!

It took me awhile, but after re-reading your reply "you need curly braces on both the function and the if statement." and looking at the code I got it!

Thank you!


#14

no problem, my bad that it was confusing.


#16

This topic is solved.