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 code
and key
variables are declared in hidden code. code
will always have the value 42
, and key
uses pickRandom()
to choose between 'yes'
or 'no'
. The two print()
statements at the top of the code will print these values out.
To complete the puzzle, edit the 2nd if statement’s test to check if key === 'yes' && code === 42
.
Sample code solution:
(Tap below to reveal)
print('The code is ' + code);
print('Key found: ' + key);
if (key === 'no' && code === 42) {
print('Unlock unsuccessful.');
}
if (key === 'yes' && code === 42) {
print('Unlock successful');
}
JavaScript Concepts: Logical Operators (&&), Assignments, Calling Functions, Array Expression
Grasshopper Concepts: print()
Additional Code (hidden code that runs before the puzzle’s code):
var code = 42;
var key = pickRandom(['yes', 'no']);