AND Operator (CONTROL FLOW)


#1


Q:
Create two variables, hungry and foodHere, and set them both equal to true. Inside the eat function, create an if statement that returns true only if both hungry and foodHere are true, and false otherwise. The function eat should take no input and hungry and foodHere should both be globals.


ERROR : SyntaxError: expected expression, got keyword 'else'


var hungry=true;
var foodHere=true;


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


#2

Here:

Not so necessary. You are going to check that truth statement in your condition. The below should suffice.

if (hungry && foodHere)

#3

Thankyou for the reply.

I did as you instructed but still getting the same error .

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


#4

Okay, you should remove the first curly brackets you closed on if return statement and put at the end to have:

var eat = function() {

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

#5

THANKS a lot! That helped although i dont understand why wont it accept the curly brackets which are normally used after the if statement. :smile:


#6

Normally? The instance of use is what matters. The function was to check if that was true; or, it returns false if it's not. More like an alternative.

Hope this helps! :slight_smile:


#7

when do you not use curly brackets on the if statements? So confused


#8

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