More on Control Flow in JS - 11 - And


#1


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



var hungry = true;
var foodHere = true;

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


#2

changed "console.log" to "return"

var hungry = true;
var foodHere = true;

var eat = function() {
if (hungry && foodHere){
return("True");}
else (!(hungry) && foodHere);{
return ("False")}
};
eat();


#3

The hint is in the error message... Instead of returning a string, return a boolean.

return true;

return false;

#4

ahh, good man
thank you Sir.


#5

@ryangkistner,

The if/else syntax can be seen like this:

if (condition) {
    // if condition is true
    // do this code
} else {
    // do this code instead
}

Now compare the above syntax to your code, see if you have added a wrongful (unnecessary condition).

And don't forget to follow @mtf suggestion as well. Use return boolean.

You're facing roughly the same issue with the syntax understanding.
I would really hope you can refer this topic (it's long but those explanations apply to your case):

I notice your problem still persist in another topic as well, your another question:

where @ghostlovescore had also provided a detailed explanation for you, however, you still did not catch the points.

I would suggest to refer back those exercises you had taken for if/else statements. Study it thoroughly and you might understand what both of us have been talking about real soon.

I'm sure if you look closely, you will notice there are some differences between the correct syntax versus your codes. Give it some time. Cheers! :slight_smile:


#6

thank you for point backwards, doing my best to understand.
I clarify condition within the if statement, using another if statement?

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


#7

I don't understand why do you need two if's?

The lesson:

Inside the eat function, create an if statement that returns true only if both hungry and foodHere are true, and false otherwise.


#8

Hmm, I will go back and re-take a look at if/else.

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

&

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

I did a flip of my condition and swapped my returns.
just to verify the code wasn't plotting the first seen return, I feel that is why some of my success in previous exercises has happened.
My code is being read, just not read properly.
or I find myself doing redundant work.


#9

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