12.Function If / else What I am doing wrong?


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

sleepCheck (7);

It said syntax error unexpected token else


#2

The errors relate to misplaced semi-colons, and missing braces.

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

sleepCheck (7);

To review,

var foo = function ( param ) {
    // code statements in a function expression
    // with one formal parameter
};

    if ( condition ) {
        // code statements for true
    } else {
        // default code statements
    }

Notice that the if and else statements don't have semi-colons anywhere in their syntax. Only the statements in their code blocks will have those, as required.


#3

Read the commented sections

var sleepCheck = function (numHours) { 
    if (numHours >= 8); // You didn't start the {} parenthesis here 
        return ("You're getting plenty of sleep! Maybe even too much!");
    }; // Don't put the semi-colon here
    else
    {
        return ("Get some more shut eye!");
    };

sleepCheck (7);

SO the structure of the final code should be like this

var functionName = function (parameters) {
    if (condition) {
        // Your code goes here;
    } else {
        // Your code goes here
    };
};

#4

An aside about return ()

I don't know much about the dictates of production code in a richly collaborative environment so cannot speak for or against wrapping return values in parens, especially when they are already quoted strings with ample demarcation.

return true;
return "some string";
return myVar;

It is common to see ()'s wrapping these values, but it is also unnecessary. JavaScript recognizes the return parameter with or without the brackets. I only eventually realized that in particular collaborative environments a coding rule might exist. Before, I thought it was wasteful and folly. Now, I see it as a way to describe the return value AS a parameter, even though return is not a function.

But, and that is a big 'but', it is still unnecessary. That is where I stand, and wait for somebody to step forward who will give us the real goods.


#5

thnx for the reply but still can;t figure it out, can you please give me the exactly right code that will work for me? I have tried lots of changes and tired of it.
Finally I type this

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 7


#6

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


#7
sleepCheck(10);
sleepCheck(5);
sleepCheck(8);

#8

Thank you and sorry to disturb you repeatedly but I am surprised it is still says
Oops, try again. There was a problem with your syntax.
SyntaxError: Unexpected token else

I wrote this

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


#9

I copied your code beneath my code and telly them, they are similar, when I run your code it runs perfect but when I run my code it keep displaying error massage even i put the spaces and places of the braces and colons where you put that, I am not getting what is the real problem


#10

Problem, as {{ is not a normal construct.


#11

Oh ! I got this
So nice of you..


#13