11. and


#1



https://www.codecademy.com/en/courses/javascript-beginner-en-qDwp0/2/2?curriculum_id=506324b3a7dffd00020bf661


In the exercise I get an error telling me i didn't declare my variable 'hungry'. I think i'm doing it correctly but i keep getting this message saying I'm not declaring the variable. Please tell me if anything is amiss.

var hungry = true;
var foodHere = true;

var eat = function() {
  // Add your if/else statement here!
  if (hungry && foodHere = true) {
      return true;
  } else {
      return false;
  };
    
  
};
eat();


#2

Change the equal sign "=" to "===" three equal signs. When saying one variable is equal to another, you use "===". When sitting something inside something else, use "="


#3

AH thank you, was so wrapped up in the 'declare hungry as a var' i kept thinking it meant the global variable... Thank you so much


#4

@tomhinton the above code works, but i got mine to work by leaving out the (=== true) since (hungry && foodHere) evaluates to true by default with &&.

// Declare your variables here!
var hungry = true;
var foodHere = true;

var eat = function() {
// Add your if/else statement here!
if (hungry && foodHere) {
return true;
}
else {
return false
}

};


#5

It works all good and lets me pass to the next question. But what worries me is when you actually call the eat(); it returns true. Isn't it supposed to return false when you call it on without mentioning any of the variables? I just don't get it and it doesn't feel good. :frowning:


#6

Getting the following error for this code: "Oops, try again. It looks like your function doesn't return true when hungry and foodHere are both true."

On running the output returned is, in fact true.

var hungry = true
var foodHere = true

var eat = function(hungry, foodHere)
{
// Add your if/else statement here!
if(hungry && foodHere === true)
{
return true;
}
else
{
return false;
}
};
eat(hungry, foodHere)