javascript · 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.
Imagine skipping with a rope. You do not write down every jump. You say jump 10 times and off you go. A loop is the same idea: write the lines once, and JavaScript repeats them.
for (let lap = 1; lap <= 3; lap++) has three parts, split by semicolons: where counting starts, the check to keep going, and what happens after each turn. lap++ means : add one to lap.
The lines inside the curly brackets are the loop's block, and they repeat every turn. The first line after the closing } runs once, after the loop has finished.
for (let lap = 1; lap <= 3; lap++) {
console.log("Lap", lap);
}
console.log("Finished!"); Change the 3 to 100 and the loop runs 100 times, with no extra typing.
guess first
Where does the counting stop? Guess first.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
for (let number = 1; number < 5; number++) {
console.log(number);
}
your turn
Your turn. Lead the crowd in a chant.
Type how many times to repeat the chant (at least 3) in the first blank, and your chant between the quotes. Then press run.
fix it
The frog jumps twice, not three times. Fix it!
- Count the Ribbits. The frog should jump three times before the splash.
jump < 3stops before 3. You want the turn wherejumpis 3 as well.- Change
<to<=in the check, then press run.
nailed it
you made the computer repeat itself. :D
- A
forloop repeats its block a set number of times. - Its three parts are: start, keep going while, step.
lap++incrementslap, adding one each turn.
new words in your collection
- loop
- for
- increment
This lesson isn’t saved. A free account remembers every lesson you finish.