×

typescript · level 1 · unit 5 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A loop keeps going while something is true.

A while loop has a condition, like an if. While the condition is true, its block runs, then it checks again. As soon as it is false, the loop stops.

You make the counter yourself: a box before the loop, a test in the brackets, and a step inside the block. It is a for loop with its parts spread out.

Forget the step and the condition stays true forever. That is an : it never ends, like a song stuck on repeat. Make sure something inside changes.

let knocks: number = 1;
while (knocks <= 3) {
  console.log("Knock knock", knocks);
  knocks++;
}
console.log("Who's there?");

If a program seems stuck, look for a loop whose condition can never turn false.