typescript · level 1 · unit 5 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A does the same job again and again.
A loop repeats a block of code. Picture laps of a running track: same route, again and again, until you have done enough. A for loop is a loop that counts its own laps.
Its round brackets hold three parts, split by semicolons. let lap = 1 is where to start. lap <= 3 is the keep-going test. lap++ is the step: adds one to the box.
Each lap, the block inside the curly brackets runs, and lap holds the number of that lap. When the test turns false, the loop ends and the program carries on below.
for (let lap = 1; lap <= 3; lap++) {
console.log("Lap", lap);
}
console.log("Finished!"); No type label is needed in let lap = 1, because TypeScript can see that 1 is a number.
guess first
Where does the count start? Guess first.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
for (let count = 0; count < 3; count++) {
console.log(count);
}
your turn
Your turn. Cheer as many times as you like.
Choose how many cheers you want, a number from 2 to 10, and type it in the blank. Then press run.
fix it
One frog, three splashes? Can you fix it?
- Splash! appears three times. Which lines are inside the curly brackets?
- Everything inside the curly brackets repeats. The splash should happen once, after the loop.
- Move the Splash! line below the closing }, then press run.
nailed it
your code can repeat itself now. :D
- A
forloop repeats its block a set number of times. - Start, keep-going test, step: three parts split by semicolons.
++adds one to the number in a box.
new words in your collection
- loop
- for
- ++
This lesson isn’t saved. A free account remembers every lesson you finish.