Set hungry to true?


#1


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


It keeps telling me to set hungry to true, but I already have!

"Oops, try again. Did you set hungry equal to true?"


 
var hungry = true;

var foodHere = true;


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


#2

var hungry = true;
var foodHere = true;

var eat = function(hungry,foodHere) {
// Add your if/else statement here!
if(hungry && foodHere){
console.log("true");
}else{
console.lgo("false");
}
};


#3

I believe the problem is in the "if" statement.

The && command already checks to see if both are true, so the =true part isn't necessary. It confuses the computer because the true command is checked twice. Thus, the conditional after the if should be (hungry && foodHere) just excluding the =true statement.


#4

That makes a lot of sense. Thanks!


#5

var hungry = true;

var foodHere = true;

var eat = function() {
if (hungry && foodHere == true) {<---ur using the assignment operator here instead of the equal to "=="
return true;
} else {
return false;
}

};

console.log(eat());

Your codes not bad, just requires a little bit of mods
and don't forget to call your function in a console.log() since you return values

(true && true ) ==> true
(true && true == true) ==> true;
(Hungy == foodHere) ==> true;

The logic is the same either ways your correct. I much prefer the latter.


#6

Thanks! Your method would work too but I've actually found a more efficient
way for this code to work. Instead of adding a == sign, you can just write
(hungry && foodHere)! This is because the && sign automatically checks for
similarities between 2 variables. Once it does that it's value will be
changed to true. The clarification of the && statement being true is
included in the and statement, therefore there is no reason to write ==.
So the code would look like this:

var hungry = true;

var foodHere = true;

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

Also, I wasn't trying to log true to the console, I was just trying to do
what the instructions said and they said to return the number, so the
values were saved in the function.
Nevertheless your code was not wrong in anyway. Thanks for the help though!


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.