Reminder: a while loop repeats as long as its condition is true. We walk laps of 6 km and print the total after each lap, until it passes 30 km:
let distance = 0;
while (distance < 30) {
distance = distance + 6;
console.log("Walked a total of " + distance + "km");
}Walked a total of 6km Walked a total of 12km Walked a total of 18km Walked a total of 24km Walked a total of 30km
distance < 30. A loop states when to keep going, not when to stop.if inside while
A bank deposit of 10000 is multiplied by 1.2 every two years. After how many years does it pass 50000? We add a year each turn, but multiply the money only in even years. A year is even when its remainder after dividing by 2 is zero: year % 2 == 0.
let money = 10000;
let year = 0;
while (money < 50000) {
year++;
if (year % 2 == 0) {
money = money * 1.2;
}
}
console.log("It will pass 50000 in " + year + " years");It will pass 50000 in 18 years
continue
The World Cup is held every 4 years starting in 2026. Suppose the 2050 tournament is cancelled and we do not want to count it. The word continue skips the rest of the current turn and goes straight back to the condition for the next turn.
let year = 2026;
let count = 0;
while (year <= 2100) {
if (year == 2050) {
year = year + 4;
continue;
}
count++;
year = year + 4;
}
console.log("There will be " + count + " tournaments by 2100");There will be 18 tournaments by 2100
year before continue. If you forget, year stays 2050, the loop skips everything on every turn, and never ends. Also, continue does not stop the loop; it skips just one turn.Practice
Complete the loop: as long as distance is less than 100, add 6 to it, then print the total: Walked a total of 6km, then Walked a total of 12km, and so on up to Walked a total of 102km.
let distance = 0;
___ (___) {
___;
console.log("Walked a total of " + distance + "km");
}Press Run to see the result. The deposit starts at 20000 and is multiplied by 1.2 every two years. Complete it: repeat as long as money is less than 50000, and add 1 to year each turn.
let money = 20000;
let year = 0;
while (___) {
___;
if (year % 2 == 0) {
money = money * 1.2;
}
}
console.log("It will pass 50000 in " + year + " years");Press Run to see the result. Print the Olympic years from 2024 to 2060: a year is an Olympic year if its remainder after dividing by 4 is 0.
let year = 2024;
while (year <= 2060) {
if (___) {
console.log(year + " is an Olympic year");
}
year += 1;
}Press Run to see the result. Add 7 to num as long as it is less than 100, count the turns in count, then print count.
let num = 0;
let count = 0;
while (___) {
num += 7;
count++;
}
console.log(count);Press Run to see the result. At the start of the loop, add: if a == 5, add 1 to a, then skip the rest of the turn with continue. It should print a: 11 times then b: 10 times.
let a = 0;
let b = 0;
while (a <= 10) {
b = b + 1;
a = a + 1;
}
console.log("a: " + a + " times");
console.log("b: " + b + " times");Press Run to see the result. Students 1 to 8 line up for the class photo, but students 3 and 6 are absent. Use while and continue to print Student 1, Student 2 ... for those present only.
Hint
Increase student at the top of the loop, so the increase never gets skipped bycontinue.let student = 0;
Press Run to see the result. Mariam saves 50 pounds every day, but on every seventh day (a day whose remainder after dividing by 7 is 0) she spends 30. Repeat while money is less than 500, then print Day 11 (the day she reached it).
let money = 0; let day = 0;
Press Run to see the result. Key points
- You can put an if inside a while; it is checked on every turn.
- "Until it passes X" is written in a while as "as long as it is less than X".
continueskips the rest of the current turn and starts the next one; change the condition variable before it.