Operator &&


#1

how do you do this instructions?

Instruction:
Create two variables, hunger and nourritureIci, and give them to true. In the function room, create an if statement that returns (return) true only if hunger and nourritureIci are both true, and false otherwise. The dining function will take two parameters - and hunger and nourritureIci - which must both be global variables.

me i realized this code for this instruction:

var hunger = true;
var foodhere = true;

var eat = function(fain, nourritureIci) {

if (hunger && foodhere) {
  return true;
}

  else if (hunger && foodhere){

    return false;

}

};

eat();

console.log(eat);

Show me your code so i understand . think!


#2

you dont need else if here.
If you have more than one condition then you should use else if
and
i cant understand why did you set condition like this.it doesn't make snese.

if (hunger && foodhere) {
  return true;
}
else if (hunger && foodhere){
 return false;
}

In your code both condition are same.but you want different result.how could it possible?
you should use different condition and different result.isn't it?
if first condition doesn't match then it'll check next one .
so your code should be like

if (hungry && foodHere) {
        return true;
       } else {
          return false;
          }
 };

and read the instruction again :slight_smile:
The function eat should take no input

so remove fain, nourritureIci from
var eat = function(fain, nourritureIci) {

i hope u've understand :slight_smile:


#3

parameter are in french but in english that (hunger,foodHere)


#4

can you please give me the lesson link ?


#5

ok https://www.codecademy.com/en/courses/javascript-beginner-fr-FR-b3xz8/2/3?curriculum_id=5355c2b7516bcbd912000008


#6

you gave me wrong link. i am talking about this lesson

in that lesson your code should be like

// DĂ©clarez vos variables ici !
faim=true;
nourritureIci=true;

var manger = function() {
    // Ajoutez votre déclaration if/else ici !
if(faim && nourritureIci ){
    
    return true;
    }else{
        
        return false;
        }
};
manger()

did you pass this lesson?


#7

no i win this lesson and i pas then . Your answer was too long. But thinks!


#8

hii,m having a problem in the code,though i think i have written it correct.here it is -

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

it is showing the error- Oops, try again. It looks like your function doesn't return false when hungry is false.what actually is it trying to say?


#9

In your return statements, you are returning text strings "true" and "false" . The instructions expect you to use boolean values true and false (without quotation marks).


#10

From the error instruction, it seems that you are supposed to give hungry the value of "false" while foodHere is given the value of true.
Declare this...
var foodHere = true;
var Hungry = false;

biff75 may be correct, but i'm only answering based on the error massg you gave.

also like biff75 said remove the quotes. You're required to enter a boolean not a string.


#11

okk..yao i got my mistake...thnxxx


#12

nopes...in instructions it is mentioned to declare both variables with true...bt thnxxx...