Unexpected token else - 11.And


#1

var hungry = true;
var foodHere = true;

var eat = function() {
if (hungry && foodHere === true)
return true;
} else {
return false;

};


#2

@michellebriffault,
You must be aware that a pair of curly-brackets-{ }
encapsulate a so-called code-block....
The IF statement with its condition has a code-block
and the ELSE statement has its own code-block

if ( condition ) {
   //IF code-block
}
else {
   //ELSE code-block
}

#3

Isn't that exactly what I've done? Why is it saying "unexpected token else"?


#4

No you didnt do it exactly
look at my comment

var eat = function() {  
if (hungry && foodHere === true)   // where is your curly bracet here ???  
         return true;
} else {  
         return false;
}

#5

Ok I finally figured it out. I needed another pair of brackets enclosing if and else.

var hungry = true;
var foodHere = true;

var eat = function() {
if (hungry && foodHere === true) {
return true
}
else {
return false
}
};


#6