The aim of this puzzle: Use the “and” logical operator &&
inside the if statement to check if 2 separate conditions are both true.
Walkthrough of the solution: The &&
logical operator is used to check if 2 separate conditions are true. It returns true only if both conditions are true. For example:
if (day === 'weekday' && holiday === 'no') {
print('You have to go to work today');
}
The if statement checks to see if day
is equal to 'weekday'
AND if vacation
is equal to 'no'
. The string 'You have to go to work today'
will be printed only if both conditions are true. If 1 or both conditions are false, then the code in the if statement’s code block will not run.
In this puzzle, the weather
variable has the value 'rainy'
, and the temp
variable uses pickRandom()
to choose between the strings 'warm'
and 'cold'
.
The 1st if statement uses the “and” operator &&
to check if weather
is equal to 'rainy'
AND if temp
is equal to 'cold'
. If both are true, the message 'Wear a rain jacket'
will be printed to the console.
Edit the 2nd if statement to check if weather
is equal to 'rainy'
AND if temp
is equal to 'warm'
.
Sample code solution:
(Tap below to reveal)
var weather = 'rainy';
var temp = pickRandom(['warm', 'cold']);
if (weather === 'rainy' && temp === 'cold' ) {
print('Wear a rain jacket');
}
if (weather === 'rainy' && temp === 'warm') {
print('Bring an umbrella');
}
JavaScript Concepts: Logical Operators (&&), Assignments, Calling Functions, Array Expression
Grasshopper Concepts: print(), pickRandom()