Aim of the puzzle: Edit an if statement.
Walk through of solution: If statements are used to run a block of code only if a certain condition is true. It looks like this:
if (x === 5) {
print(x)
}
Inside the ()
is the test. It uses the equality operator ===
to check if x
is equal to 5
. If this is true, then the code inside the {}
will run, and x
will be printed to the console. If x
is not 5
, then the program will skip over the if statement, and the code inside the {}
will not run.
In this puzzle, the value
variable uses pickRandom
to choose between the strings 'oi'
and 'hola'
. The variable is then printed to the console, so we can see which was picked.
The 1st if statement checks if value
is equal to 'hola'
. If this is true, then the string 'Hi in Spanish'
will be printed.
The 2nd if statement will check if value === 'oi'
. If this is true, then the string 'Hi in Portuguese'
will be printed.
Sample code solution:
var value = pickRandom(['oi', 'hola']);
print(value);
if (value === 'hola') {
print('Hi in Spanish');
}
if (value === 'oi') {
print('Hi in Portuguese');
}
Javascript Concepts: If Statements, Strings, Variables
Grasshopper Concepts: print()
, pickRandom()