12. functions & if/else


#1

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

SyntaxError: Unexpected token else


#2

Once is enough and a new thread for a new question is better.

Your problem is that you messed up the curly braces. The syntax of conditions is:

if(condition){
    code
}
else{
     otherCode
}

compare this to your code.


#3

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));
SyntaxError: Unexpected token if
i still don't get it


#4

better but now you still lack the {} for the function that wrap up the if/else.


#5

How about you just tell us what the answer is and we can figure it out from there...


#6

How about no.
Seriously, first of all its against the forum policy to post uncommented solutions and after all I don't consider it that much helpful either. The exercises are rather short so in the end they all look pretty similar and if you couldn't figure out the difficulty beforehand chances are good you will still not find it when comparing you're code to "the solution". Also this often leads to a copy&paste pass without grasping the necessary knowledge. Also the whole idea of programming is kind of like:

You have a task, some requirements and a tool set of option to pick from. Solve the task and fulfil the requirements. Good Luck!

So first of all there might not even be "the solution" but a whole bunch of ways that work (more or less) and also solving puzzles and making mistakes is part of the progress. It's not something evil but almost inevitable and might even enhance your understanding if you find out why something is wrong.


#7

After starting if condition u did not open braces bt u closed. thats y


#8

I have a similar issue, however the result prints out. Still showing error
var sleepCheck = function (numHours)
{
if (numHours >= 8)
{
return "You're geting plenty of sleep! Maybe even too much!";
}
else {
return "Get some more shut eye!";

}

}

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


#9

Seriously struggling with this one! Please help! It's a syntax error involving an illegal return statement.

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


#11

write corect

else(xxx)
{
    (code);
}

and in beginning after function=(xxxx) open {
in the and you must close };
you close else and funtion like that
}
};


#12

Not Needed here

You also need to call sleepCheck(10);
sleepCheck(5);
sleepCheck(8);


#13

@lienevitamante
your code seems to be correct, from a syntax point of view. So probably it is a typo in your output messages. This is the point where copy&paste is a valid option :slightly_smiling:
Also you might consider wrapping your function calls in console.logs otherwise you only see the last of them due to a console echo.

@nerincker
var sleepCheck = function(numHours) {return numHours};
this is your function and the rest is outside of it. So maybe reread the instruction.


#14

var sleepCheck=function(numHours) {

if (sleepCheck >= 8) {
    return ( " you 're getting plenty of sleep! May be even too much!");
}
else 
{
    return ("get some more shut eye!");
}

}

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

error:"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."


#15

"You" with upper case Y and without space between u and '.
And of course this "real" issue:

if (sleepCheck >= 8) {

of using the function name here.


#16

var sleepCheck=function(numHours) {
return numHours
}
{
if (sleepCheck >= 8) {
console.log (" You're getting plenty of sleep! May be even too much!");
}
else
{
console.log ("get some more shut eye!");
}

};
result
"get some more shut eye!"
10

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

error:
" 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."


#17

I´m sorry for my manners, This is fabio here, and this is having me struggle very hard


#18

Not needed.

Not sure if this is there or not, get rid off it.

Dont give that a } till you're about to enter the sleepchecks.

Not sure if neccesary but put sleepCheck inside a console.log

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

@haxor789 anything else I missed?


#19

var sleepCheck=function(numHours)

if (sleepCheck >= 8) {
console.log (" You're getting plenty of sleep! May be even too much!");
}
else
{
console.log ("get some more shut eye!");
};

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

SyntaxError: expected expression, got keyword 'if'
Oops, try again. There was a problem with your syntax.


#20

Guess that is what he sees on the console. Which is surprising as I'd expected to see:

"get some more shut eye!"
8

Short summary of what is happening:
function returns numHours instead of checking including the if/else. (That's where the number comes from).
if/else uses sleepCheck instead of numHours -> [Function] >= 8 -> else case is chosen

And last but not least uses console.log instead of return. Which only works if the if/else is inside of the function.


#21

Put all this in a set of { } put the { after

and then another } after your last }

Then call the function like this