11. And (Just and)


#1

What am I doing wrong?
var hungry = true;
var foodHere = true;

var eat = function() {

if (hungry && foodHere = true) {
return true;
}
};


#2

remove the = true from foodHere

it should look like this

if (hungry && foodHere)


#3

Thanks but it still doesn't work
var hungry = true;
var foodHere = true;

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


#4

Try this

var hungry = true;
var foodHere = true;

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


#5

It works! Thanks
:grin:


#6

Thanks! This has been driving me crazy! One thing I want to know though, // "What're those!"//(It rhymes!) why do you need the return true/false?


#7

I did similar thing but its considered wrong...

var hungry = true;
var foodHere = true;

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

This way is just an alternative way right?


#8

Instead of using else if, why not use else?
like:

else{
return false;
}

And one more thing, you forgot to close your curly bracket on your if statement.
Add close curly bracket for your if statement before your else statement or right after this line: return true;


#9

i just wanna know why we didn't take any parameters in the function .. what does it mean .. and when can i dont put any parameters in the function ?


#10

you do not have to put any parameters in any function. it is totally up to you.

for example you can have a function

var name = "ford";
var model = function (name){ var car_model = name;}


#11

because the else can have the following possibilities:

(!hungry && foodHere)

(hungry && !foodHere)

you place exactly what you want to test inside the if