typescript · level 1 · unit 5 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Put an if inside a loop to check every number.
A loop can hold any code, even an if. Each lap, the if asks its question again with the new number, so the loop checks them one by one.
% gives the : what is left over after dividing. 7 % 2 is 1, because two goes into seven three times with one left. Even numbers leave nothing, so for them % 2 gives 0.
The if block sits inside the loop block, so it is indented two more spaces. Each closing curly bracket lines up with the line that opened it.
for (let house = 1; house <= 6; house++) {
if (house % 2 === 0) {
console.log("Letter for house", house);
}
} Read house % 2 === 0 as “house divides by 2 with nothing left over”.
guess first
Which numbers make it through? Guess first.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
for (let n = 1; n <= 6; n++) {
if (n % 3 === 0) {
console.log(n);
}
}
your turn
Your turn. Play buzz.
Choose how far to count in the first blank, like 20. In the second, choose the number to buzz on, like 5. Then press run.
fix it
The post is stuck in the van. Fix the check!
- No letters are posted at all. Look at the sign inside the
if. /divides, sohouse / 2is never 0. You want what is left over instead.- Change / to % on line 2, then press run.
nailed it
your loops can make choices now. :D
- An
ifinside a loop checks every lap. %gives the remainder after dividing.- A number is even when
% 2gives0.
new words in your collection
- remainder
This lesson isn’t saved. A free account remembers every lesson you finish.