Choosing a for loop or a while loop (JavaScript)

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

Choosing a for loop or a while loop (JavaScript)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. You want to print the numbers 1 to 10. Which loop is the best choice?
A for loop, because you know it repeats 10 times
B while loop, because it can never be used for counting
2. A program keeps asking 'What is the password?' until the user types the right answer. Which loop fits best?
A while (answer !== password) { ... }
B for (let i = 0; i < 10; i++) { ... }
3. True or false: Any for loop can be rewritten as a while loop.
A False
B True
4. Which while loop does the same job as: for (let i = 1; i <= 5; i++) { console.log(i); }
A let i = 1; while (i <= 5) { console.log(i); i++; }
B let i = 1; while (i <= 5) { console.log(i); }
5. A game repeats a round exactly 3 times. Which loop is easier to read?
A while (true) with a counter hidden further down
B for (let round = 1; round <= 3; round++)

Match the Pairs

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

Left columnRight column
1for (let i = 1; i <= 10; i++) { Afor (let i = 1; i <= 10; i++) { console.log(i); }
2while (answer !== "blue") { BA for loop is a poor fit here because the number of repeats is unknown and the counter i is never used. A while loop (while (guess !== "7")) is clearer.
3Any for loop can be rewritten as a while loop by moving the setup before the loop and the update inside it. CA while loop is a good choice here because we repeat until a condition changes, and we do not know how many tries it will take.
4Use a for loop for the readable choice when printing the numbers 1 to 10, because the counting is all in one place. Dlet i = 1; while (i <= 10) { console.log(i); i++; }
5let guess = prompt("Number?"); for (let i = 0; guess !== "7"; ) { guess = prompt("Number?"); } EThe loop never updates i, so it runs forever. Add i++ inside the loop, or use a for loop for a known count of 10.
6let i = 1; while (i <= 10) { console.log(i); } FA for loop is a good choice here because we know exactly how many times to repeat: 10 times.

Definitions

for vs while

Fill in the Blanks

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

1.
___ (let i = 1; i <= 10; i++) { console.log(i); }
2.
let ans = ""; _____ (ans !== "yes") { ans = prompt("?"); }
3.
let i = 1; while (i <= 10) { console.log(i); ___; }
4.
for (let i = 1; i __ 5; i++) { console.log("Hi"); }
5.
let total = 0; for (let n = 1; n <= _; n++) { total += n; }

Spot the Error

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

Question 1
A let n = 1;
B while (n <= 10) {
C console.log(n);
D n--;
E }
Question 2
A let guess = prompt("Guess?");
B while (guess !== "7") {
C prompt("Again?");
D }
E console.log("Correct!");
Question 3
A let count = 0;
B for (let i = 0; i < 10; i++) {
C count + 1;
D }
E console.log(count);
Question 4
A let total = 0;
B let n = 5;
C for (let i = 1, i <= n, i++) {
D total += i;
E }
Question 5
A let answer = "";
B while (answer = "yes") {
C answer = prompt("Ready?");
D }

Answer Key

Choosing a for loop or a while loop (JavaScript)
Section A: Quick Questions
Q1
A — for loop, because you know it repeats 10 times
Q2
A — while (answer !== password) { ... }
Q3
B — True
Q4
A — let i = 1; while (i <= 5) { console.log(i); i++; }
Q5
B — for (let round = 1; round <= 3; round++)
Section B: Match the Pairs
Q1
for (let i = 1; i <= 10; i++) { → A for loop is a good choice here because we know exactly how many times to repeat: 10 times.
Q2
while (answer !== "blue") { → A while loop is a good choice here because we repeat until a condition changes, and we do not know how many tries it will take.
Q3
Any for loop can be rewritten as a while loop by moving the setup before the loop and the update inside it. → let i = 1; while (i <= 10) { console.log(i); i++; }
Q4
Use a for loop for the readable choice when printing the numbers 1 to 10, because the counting is all in one place. → for (let i = 1; i <= 10; i++) { console.log(i); }
Q5
let guess = prompt("Number?"); for (let i = 0; guess !== "7"; ) { guess = prompt("Number?"); } → A for loop is a poor fit here because the number of repeats is unknown and the counter i is never used. A while loop (while (guess !== "7")) is clearer.
Q6
let i = 1; while (i <= 10) { console.log(i); } → The loop never updates i, so it runs forever. Add i++ inside the loop, or use a for loop for a known count of 10.
Section C: Definitions
Q1
for vs while — Use for when you know how many times to repeat, like 1 to 10. Use while when repeating depends on a condition, like asking until the answer is correct.
Section D: Fill in the Blanks
Q1
for (let i = 1; i <= 10; i++) { console.log(i); }
Q2
let ans = ""; while (ans !== "yes") { ans = prompt("?"); }
Q3
let i = 1; while (i <= 10) { console.log(i); i++; }
Q4
for (let i = 1; i <= 5; i++) { console.log("Hi"); }
Q5
let total = 0; for (let n = 1; n <= 5; n++) { total += n; }
Section E: Spot the Error
Q1
D — n-- makes n smaller, so n <= 10 is always true and the loop never ends. A while loop must change its variable towards ending, so it should be n++;.
Q2
C — The new answer is never stored in guess, so the condition never changes and the loop keeps asking forever. It should be guess = prompt("Again?");.
Q3
C — count + 1; works out a value but never saves it, so count stays 0. It should be count = count + 1; (or count++;).
Q4
C — The three parts of a for loop header are separated by semicolons, not commas. It should be for (let i = 1; i <= n; i++) {.
Q5
B — A single = assigns a value instead of comparing. The condition should compare with !==, like while (answer !== "yes") {, so the loop keeps asking until the answer is yes.