12. Functions & if/else. What's missing?


#1

So I think I've done everything right, but it keeps saying: SyntaxError: Unterminated string constant
help would be greatly appreciated

code:
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(5);


#2

I fixed it a little but now it says: It looks like your function doesn't return "Get some more shut eye!" when numHours is 5. Check your if / else syntax and whether you're using the correct comparison operator.

new code:

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


#3

Is => a valid comparison operator in javascript?


#4

No biff, => isn't a valid comparison operator. It is actually >=. I remember it because it comes in order. Its pronounced "Greater than or equal to" so the greater than sign comes first and then the equals sign. The same logic applies to "Less than or equal to".

Regards,
Barry Allen


#5

I was trying to help @evildrak remember without telling him :smile:


#6

I am sooo sorry biff. I thought you were the person that had posted the topic. I am truly sorry for the misunderstanding.

Regards,
Barry Allen


#7

No worries @bartholomewallen


#8

Thanks guys. Even if @bartholomewallen didn't say the answer I would've figured out the hint anyway. I appreciate the help though! :smile:


#9

sleepCheck = function (numHours)
{
//sleepCheck(10);

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

};
sleepCheck(10);
it always shows this"SyntaxError: Unexpected token {
"


#10

Remove the sleepCheck in this statement


#11

And the bracket opening the if statement comes after the condition,

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

#12

error is still same , after removing sleepCheck


#13

Let me see your updated code


#14

var sleepCheck = function (numHours)
{
//sleepCheck(10);



    (numHours>=8);
    {
    return("You're getting plenty of sleep! Maybe even too much!");
}
else

    //(numHours<8);
    {
    return( "Get some more shut eye!");
}

};
sleepCheck(10);


#15

sorry , refer to this code
sleepCheck = function (numHours)
{
//sleepCheck(10);


if
    (numHours>=8);
    {
    return("You're getting plenty of sleep! Maybe even too much!");
}
else

    //(numHours<8);
    {
    return( "Get some more shut eye!");
}

};
sleepCheck(10);


#16

var sleepCheck = function (numHours)
{
//sleepCheck(10);


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

};
sleepCheck(10);

It is showing this " SyntaxError: Unexpected token else"


#17

Can someone let me know what is wrong with the below please? The error message is saying "SyntaxError: Unexpected token else"

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


#18

Hello @jameshorsman,

you have put a semicolon at the end of your if statement. Replace the semicolon with a opening curly brace and end the function with a curly brace like so..

var function_name = function(arguments) {
    if (condition) {
        
    } else {

    }
};

#19

Thanks a lot for your help!


#20

hey there, I need help regarding Function & if/else topic
I keep getting syntax error. Following is the code I have written:

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)