简体   繁体   中英

Logical Operator Exercise in Codecademy: hit a wall

What is missing in the following code?

I keep trying variations and am at that point where I'm trying illogical modifications simply for lack of better ideas. Does the problem lie in the function syntax? Best I understand the variables are global, and both SHOULD read as TRUE. Depending on how I structure the function I can get a read out of TRUE AND FALSE, or false, but, if I understand Logical Operator syntax correctly, only one outcome should output to true - when both variables are true. Hence, as I set them to true, I'm a bit mystified WHY the readout is getting False.![enter image description here][1]

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

var eat = function() 
{
if (hungry&&foodHere) 
{
      console.log ("True.");
}
else
{
      console.log("False.");
  }
};

//Error message reads: It looks like your function doesn't return false when hungry is false.

I'd post the image but apparently my reputation is lacking.

CodeAcademy wants you to return a boolean value, whereas you're just logging a "True" or "False" string to the console. Change it to something like:

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

or in your case:

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

See this JSFiddle example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM