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.

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.

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.

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.

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.

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