The aim of this puzzle: Edit the classic for loop to count up by 10.
Walkthrough of the solution: A classic for loop is used to repeat a block of code a specific number of times. A classic for loop has 3 components separated by semicolons:
- A start condition that declares looping variable and gives it a value.
- A test that keeps the classic for loop running as long as the test is true.
- An update operation that changes the looping variable after each iteration (loop).
For example:
for (var i = 0; i < 10; i = 1 + 1) {
print(i)
}
-
var i = 0
sets up the looping variablei
and gives it the value0
. -
i < 10
is the test. The classic for loop will run as long asi
is less than10
. -
i = i + 1
updates the value ofi
after each iteration, adding1
to the value each time.
In this puzzle, a classic for loop has been set up that sets i
to 0
and increments i
by 5
while i < 35
is true.
To complete the puzzle, edit the classic for loop to update i
by 10
after each iteration, rather than 5
.
To do this, change i = i + 5
to i = i + 10
.
Sample code solution:
(Tap below to reveal)
for (var i = 0; i < 35; i = i + 10) {
print(i);
}
JavaScript Concepts: Code Block (for loop), Calling Functions, Variable Declaration, Identifiers, Loops
Grasshopper Concepts: print()