Javascript · Loops
Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Quiz: Counting Turns
How many times is `Go!` printed?
for (let i = 0; i < 4; i++) { console.log("Go!"); }
Options
• ```4```
• ```3```
• ```5```
• ```0```
Hint : List the values the counter takes while the condition stays true.
Exercise 2 — Quiz: Step by Step
What will this code print?
for (let i = 2; i <= 8; i += 2) { console.log(i); }
Options
• ```2```, ```4```, ```6```, ```8```
• ```2```, ```4```, ```6```
• ```0```, ```2```, ```4```, ```6```, ```8```
• ```2```, ```3```, ```4```, ```5```, ```6```, ```7```, ```8```
Hint : Apply the update to the counter after each turn and check the limit each time.
Exercise 3 — Quiz: After the Loop
What will this code print?
let i = 0; while (i < 3) { i++; } console.log(i);
Options
• ```3```
• ```2```
• ```0```
• ```4```
Hint : The value printed is the one that made the condition fail.
Exercise 4 — Quiz: Draining HP
What will this code print?
let hp = 30; while (hp > 0) { console.log(hp); hp -= 15; }
Options
• ```30```, ```15```
• ```30```, ```15```, ```0```
• ```15```, ```0```
• ```30```
Hint : Check the order of the two lines in the block, then evaluate the condition with each new value.
Exercise 5 — Quiz: One to Five
Complete the loop so it prints the numbers 1 to 5.
for (let i = ____; i ____ 5; i++) { console.log(i); }
Options: 1 <= 0 < 5 ==
Hint : Decide where the counter must start, and whether the last number you want has to pass the condition.
Exercise 6 — Quiz: A False Start
What will this code print?
let ready = false; do { console.log("Loading..."); } while (ready);
Options
• ```Loading...``` once
• Nothing at all
• ```Loading...``` forever
• ```false```
Hint : Think about when this loop checks its condition for the first time.
Exercise 7 — Quiz: Early Exit
What will this code print?
for (let i = 1; i <= 5; i++) { if (i === 4) { break; } console.log(i); }
Options
• ```1```, ```2```, ```3```
• ```1```, ```2```, ```3```, ```4```
• ```1```, ```2```, ```3```, ```5```
• ```4```
Hint : Find the turn where the inner condition becomes true, and remember what the keyword inside does to the loop.
Exercise 8 — Quiz: Skipping Odds
What will this code print?
for (let i = 1; i <= 5; i++) { if (i % 2 === 1) { continue; } console.log(i); }
Options
• ```2```, ```4```
• ```1```, ```3```, ```5```
• ```1```, ```2```, ```3```, ```4```, ```5```
• ```2```
Hint : Work out which values make the inner condition true, then remember what happens to those turns.
Exercise 9 — Quiz: Level Up
Complete the loop so Aquachu levels up until it reaches level 5.
let level = 1; ____ (level ____ 5) { level++; console.log("Level " + level); }
Options: while < for > do <=
Hint : The keyword must repeat based on a condition alone. Then ask yourself at which value the loop should stop, so the last message is the right one.
Exercise 10 — Quiz: What's Wrong?
What happens when this code runs?
let i = 0; while (i < 3) { console.log(i); }
Options
• It never stops
• It prints ```0```, ```1```, ```2```
• A syntax error
• It prints nothing
Hint : Look for the line that should make the condition change over time.
Exercise 11 — Quiz: The Sum
What will this code print?
let sum = 0; for (let i = 1; i <= 4; i++) { sum += i; } console.log(sum);
Options
• ```10```
• ```4```
• ```6```
• ```24```
Hint : Keep a running total on paper, adding the counter at each turn.
Exercise 12 — Quiz: Backwards
What will this code print?
for (let i = 3; i >= 1; i--) { console.log(i); }
Options
• ```3```, ```2```, ```1```
• ```3```, ```2```
• ```1```, ```2```, ```3```
• ```3```, ```2```, ```1```, ```0```
Hint : Follow the update direction and check whether the limit value itself passes the condition.
Exercise 13 — Quiz: Complete the do...while
Complete this loop so Pyrochu takes 10 damage at least once, then keeps taking damage while its HP is above 50.
let hp = 80; ____ { hp -= 10; console.log(hp); } ____ (hp > 50)____
Options: do while ; for if }
Hint : Two keywords frame the block in this loop, and one small character finishes the line.
Exercise 14 — Quiz: Guaranteed Run
Which loop always runs its block at least once, even if the condition is false at the start?
Options
• ```do...while```
• ```while```
• ```for```
• All of them
Hint : Think about the position of the condition relative to the block in each loop.
Exercise 15 — Quiz: Bigger Steps
How many times does the block run?
for (let i = 0; i < 10; i += 4) { console.log(i); }
Options
• ```3```
• ```2```
• ```4```
• ```10```
Hint : Write down each value of the counter until one fails the condition.
Exercise 16 — Quiz: Multiples Only
What will this code print?
for (let i = 0; i < 6; i++) { if (i % 3 !== 0) { continue; } console.log(i); }
Options
• ```0```, ```3```
• ```3```
• ```1```, ```2```, ```4```, ```5```
• ```0```, ```3```, ```6```
Hint : Find which values of the counter are NOT caught by the inner condition; those are the ones that reach the end of the block.
Exercise 17 — Quiz: Charging Up
What will this code print?
let charge = 0; while (true) { charge += 5; if (charge >= 15) { break; } } console.log(charge);
Options
• ```15```
• ```10```
• ```20```
• It never stops
Hint : Increase the value step by step and stop at the first value that satisfies the inner condition.
Exercise 18 — Quiz: Found It
Complete the code so the search stops as soon as the target slot is reached.
let target = 3; for (let slot = 1; slot <= 5; slot++) { console.log("Slot " + slot); ____ (slot === target) { ____; } }
Options: if break continue while stop for
Hint : First test the slot, then use the keyword that ends the whole loop rather than a single turn.
Exercise 19 — Quiz: Letter by Letter
What will this code print?
let name = "Zap"; for (let i = 0; i < name.length; i++) { console.log(name[i]); }
Options
• ```Z```, ```a```, ```p```
• ```Zap```
• ```0```, ```1```, ```2```
• ```Zap```, ```Zap```, ```Zap```
Hint : Remember what indexing a string with a number gives you, and how many indexes this string has.
Exercise 20 — Quiz: Spending Coins
What will this code print?
let total = 100; for (let i = 0; i < 3; i++) { total -= 25; } console.log(total);
Options
• ```25```
• ```75```
• ```0```
• ```100```
Hint : Count the number of turns first, then apply the change that many times.
Exercise 21 — Quiz: Two Conditions
What will this code print?
let hp = 50; let turn = 0; while (hp > 0 && turn < 2) { turn++; hp -= 10; } console.log(turn);
Options
• ```2```
• ```5```
• ```0```
• ```1```
Hint : With two conditions joined by AND, the loop stops as soon as either one fails. Find which one fails first.
Exercise 22 — Quiz: Count with do...while
What will this code print?
let n = 0; do { n++; } while (n < 3); console.log(n);
Options
• ```3```
• ```2```
• ```4```
• ```0```
Hint : Run the block, then check the condition, and repeat until it fails. The printed value is the last one.
Exercise 23 — Quiz: Odd Numbers Only
Complete the loop so it prints only the odd numbers from 1 to 6.
for (let i = 1; i <= 6; i++) { if (i ____ 2 === 0) { ____; } console.log(i); }
Options: % continue break / * stop
Hint : Which operator tells you whether a number is even? And which keyword skips a turn without ending the loop?
Exercise 24 — Quiz: Which Loop?
Aquachu drinks potions until its HP is full. It might not need any potion at all, and you don't know in advance how many it will need. Which loop is the most natural?
Options
• ```while```
• ```for```
• ```do...while```
• ```switch```
Hint : Two clues in the sentence matter: the unknown number of turns, and the possibility of zero turns.
Exercise 25 — Quiz: Both at Once
What will this code print?
for (let i = 1; i <= 5; i++) { if (i === 2) { continue; } if (i === 4) { break; } console.log(i); }
Options
• ```1```, ```3```
• ```1```, ```3```, ```5```
• ```1```, ```2```, ```3```
• ```1```
Hint : Go through the turns one by one and apply each keyword's effect: one skips a turn, the other ends everything.
Back to the course