Lesson 5 of 15 · about 12 minutes · for loops and while loops.
⚡ 0 XP
Step 1 of 2
The for loop
A loop repeats code so you don’t have to copy it. The for loop has three parts inside its brackets:
let i = 1 — start: make a counter
i <= 10 — keep going while this is true
i++ — after each round, add 1 to the counter
So for (let i = 1; i <= 10; i++) { ... } runs the block 10 times, with i = 1, 2, 3 … 10.
Example
for (let i = 1; i <= 3; i++) {
console.log(`Round ${i}`);
}
Prints Round 1, Round 2, Round 3
🎯 Challenge: The 7 times table +15 XP
Use a for loop to print the table of n (7), from 7 x 1 = 7 to 7 x 10 = 70 — exactly 10 lines, in this format.
Try twice to unlock the solution (0/2)
script.jsCtrl ⏎
Console
Output from console.log appears here.
Checklist
Press Run to check your code.
○
Exactly 10 lines are printed
○
The first line is “7 x 1 = 7”
○
The last line is “7 x 10 = 70”
○
A for loop is used
Lesson notes (text version)
1. The for loop
A loop repeats code so you don’t have to copy it. The for loop has three parts inside its brackets:
let i = 1 — start: make a counter
i <= 10 — keep going while this is true
i++ — after each round, add 1 to the counter
So for (let i = 1; i <= 10; i++) { ... } runs the block 10 times, with i = 1, 2, 3 … 10.
for (let i = 1; i <= 3; i++) {
console.log(`Round ${i}`);
}
Challenge: The 7 times table
Use a for loop to print the table of n (7), from 7 x 1 = 7 to 7 x 10 = 70 — exactly 10 lines, in this format.
2. The while loop
Use while when you don’t know how many times to repeat — only when to stop:
while (savings < 1000) { ... } keeps going as long as savings is below 1000
Careful: something inside the loop must change, or the condition stays true forever (an endless loop). Here, savings grows every round, so the loop ends.
let laddoos = 10;
while (laddoos > 0) {
laddoos = laddoos - 3;
console.log("Left:", laddoos);
}
Challenge: Saving for a cricket bat
Priya saves ₹150 every week. She needs at least ₹1000 for a cricket bat. Use a while loop: while savings is below 1000, add 150 to savings and 1 to weeks. Finally print Weeks: 7, saved ₹1050.