Error in exercise 12/13?


#1

I wrote this piece of code in 12/13, and it seems correct.

But, now I'm really confused, cause I thought that every if-statement AND else-statement had an opening { and closing } around it (in all my notes and previous exercises I find the { and } around it).
So, what is the right answer?

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

};

console.log (sleepCheck(10));
console.log (sleepCheck(5));
console.log (sleepCheck(8));


#2

missing } to close the function.


#3

thats not it i tried


#4

@ladyit yes its true this his how the if /else block is written

if(condition){/*statements*/}
else if(another_condition){/*statements*/}
else{/*statements*/}

But you can omit the {} when the number of statements you have in the block is just one. For example

var feeling_sleepy = prompt("Are you feeling sleepy? yes or no").toLowerCase();

if(feeling_sleepy === "yes")     console.log("Good Night");
else if(feeling_sleepy === "no") console.log("Just keep having fun");
else                             console.log("Undefined");

You can test it out here too

One more thing, this is your code corrected

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

Remember the else block takes no conditions


#5

Thanks for the replies!!
And al my spacing after if/return/etcetera ain't necessary as well?


#6

Yes if you dont include the spaces the code will run perfectly the same will happen if you did include it