11 And .. what's wrong ? help me


#1


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();


#2

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();

it worked ^^


#3

Hi @arcace00944,

It's great that you realize that you don't need parameters for your function in this exercise, so:

var eat = function() {

    // run the code

}

One thing to mention is this:

if (hungry && foodHere === true)

while your code above is correct, it is better to write it this way:

if (hungry === true && foodHere === true) OR if (hungry && foodHere)

Your code works because those variables are coincidentally assigned with true.
Your code will have different effects if the code changes to:

var hungry = false;
var foodHere = false;

var eat = function() {

    if(hungry && foodHere === false){
        //Do something
    } else {
        //Do something else 
    }
};

eat();

Because the JavaScript will interpret it as if ( hungry === true && foodHere === false ), instead of if ( hungry === false && foodHere === false )

Else, if you really want to write it your way, you should enclose it with another bracket
means

if ( ( hungry && foodHere ) === false )

Just thought it is important for you to see this. :slight_smile:


#4

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