I need a refresher on if/else


#1

i need a refresher on if /else statements

#2

if (condition) {
    block of code to be executed if the condition is true
} else { 
    block of code to be executed if the condition is false
}

Let me know if you need further explanation/ an example.


#3

i'm still uncertain on the setup


#4

Here is an example off the top of my head.

if (number < 10) {
return "This number is under 10";
}
else {
return "This number is over 10";
}

Read through this if you are still not getting it.

https://www.w3schools.com/js/js_if_else.asp


#5

how would you change it to hungry && foodHere and where do you put it


#6

Could you copy and paste your exercise URL so I can see the question?


#7

sure here it is
(i just used copy and paste so that i would have the basic outline)
var hungry = true;
var foodHere = true;

var eat = function() {
if (number < 10) {
return "This number is under 10";
}
else {
return "This number is over 10";
}
};


#8

I mean the website address (URL) so I can have a look at the exact question being asked.
Or just copy and paste the question please.


#9

sorry i got a little confused there but is this what you meant
The logical operator and is written in JavaScript like this: &&. It evaluates to true when both expressions are true; if they're not, it evaluates to false.

true && true; // => true
true && false; // => false
false && true; // => false
false && false; // => false
Instructions
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.


#10

got to go for now sorry :slight_smile:


#11

All good.

Like this.

var hungry = true;
var foodHere = true;

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

I hope this has cleared it up for you.


#12

thanks so much i got it now


#13

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