Visual repeat, forever and repeat-until blocks to JavaScript

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Visual repeat, forever and repeat-until blocks to JavaScript

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which JavaScript loop is the typed version of a 'repeat 10' block?
A while (true) { }
B for (let i = 0; i < 10; i++) { }
2. Which JavaScript is the typed version of a 'forever' block?
A while (true) { }
B for (let i = 0; i < 1; i++) { }
3. A 'repeat until score = 5' block becomes which loop?
A while (score !== 5) { }
B while (score === 5) { }
4. True or false: 'repeat until score = 5' and 'while (score !== 5)' mean the same thing.
A False: while (score !== 5) stops when score is not 5
B True: both stop once score is 5
5. Where should the counter variable go for a while loop that counts?
A let score = 0; before the while loop
B let score = 0; inside the while loop body

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1for (let i = 0; i < 10; i++) { AThe typed version of a repeat 10 block: the loop body runs 10 times.
2while (score !== 5) { Blet score = 0; while (score !== 5) { score++; }
3A forever block with an if block inside becomes an endless while loop with an if statement in its body. Cwhile (true) { if (touching) { console.log("Hit!"); } }
4A counter variable must be created before the loop so the loop condition can use it. DThe typed version of a repeat until score = 5 block: keep looping while score is NOT 5.
5while (score === 5) { score++; } EA forever block was typed as for with no loop that never ends; instead, the counter is used without being created. The variable count was never declared before it is used, so this causes a ReferenceError.
6for (let i = 0; i < 10; i++) { console.log(i); } console.log(count); FThe condition was copied directly from repeat until score = 5. A while loop needs the opposite condition, score !== 5, so this loop never runs when score starts at 0.

Definitions

while (score !== 5)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for (let i = 0; i < __; i++) { console.log("Hello"); }
2.
while (____) { if (score > 5) { console.log("Win"); } }
3.
let score = 0; while (score ___ 5) { score++; }
4.
___ score = 0; while (score !== 5) { score++; }
5.
___ (let i = 0; i < 10; i++) { console.log("Jump!"); }

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let jumps = 0;
B console.log('Start');
C // repeat 10
D for (let i = 0; i < 10; i--) {
E jumps++;
F }
Question 2
A let timer = 0;
B while (false) {
C timer++;
D if (timer === 5) break;
E }
Question 3
A let score = 0;
B // repeat until score = 5
C while (score === 5) {
D score++;
E }
Question 4
A let score = 0;
B while (score = 5) {
C score += 1;
D console.log(score);
E }
Question 5
A // repeat until count = 10
B let count = 10;
C while (count !== 10) {
D count++;
E }

Answer Key

Visual repeat, forever and repeat-until blocks to JavaScript
Section A: Quick Questions
Q1
B — for (let i = 0; i < 10; i++) { }
Q2
A — while (true) { }
Q3
A — while (score !== 5) { }
Q4
B — True: both stop once score is 5
Q5
A — let score = 0; before the while loop
Section B: Match the Pairs
Q1
for (let i = 0; i < 10; i++) { → The typed version of a repeat 10 block: the loop body runs 10 times.
Q2
while (score !== 5) { → The typed version of a repeat until score = 5 block: keep looping while score is NOT 5.
Q3
A forever block with an if block inside becomes an endless while loop with an if statement in its body. → while (true) { if (touching) { console.log("Hit!"); } }
Q4
A counter variable must be created before the loop so the loop condition can use it. → let score = 0; while (score !== 5) { score++; }
Q5
while (score === 5) { score++; } → The condition was copied directly from repeat until score = 5. A while loop needs the opposite condition, score !== 5, so this loop never runs when score starts at 0.
Q6
for (let i = 0; i < 10; i++) { console.log(i); } console.log(count); → A forever block was typed as for with no loop that never ends; instead, the counter is used without being created. The variable count was never declared before it is used, so this causes a ReferenceError.
Section C: Definitions
Q1
while (score !== 5) — Typed form of a repeat until score = 5 block: keep looping while the opposite is true. A repeat 10 block becomes a for loop, and forever becomes while (true).
Section D: Fill in the Blanks
Q1
for (let i = 0; i < 10; i++) { console.log("Hello"); }
Q2
while (true) { if (score > 5) { console.log("Win"); } }
Q3
let score = 0; while (score !== 5) { score++; }
Q4
let score = 0; while (score !== 5) { score++; }
Q5
for (let i = 0; i < 10; i++) { console.log("Jump!"); }
Section E: Spot the Error
Q1
D — The counter i goes down with i--, so it never reaches 10 and the loop never ends. A repeat 10 block becomes a for loop that counts up with i++.
Q2
B — A forever block becomes while (true). With while (false) the condition is never true, so the loop body never runs.
Q3
C — Repeat until score = 5 means keep looping while score is NOT 5. Using === 5 makes the loop stop straight away because score starts at 0. It should be while (score !== 5).
Q4
B — A single = assigns 5 to score instead of comparing, so the condition is always truthy and the loop never ends. A repeat until block needs while (score !== 5).
Q5
B — The counter variable must be set to its starting value before the loop. Starting at 10 means count !== 10 is false at once, so the loop never runs. It should be let count = 0;