انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Web: home
0. Lab tour1
1. HTML: the page's content7
2. CSS: the page's look6
3. JavaScript: the actions7
4. The project: the to-do list3
5. Pro CSS: shadows and movement5
6. Arrays and building the page with code3
7. Events and forms3
8. Objects and time3
9. Canvas: drawing and games3
10. The final project: the memory game3

The times table: for with a counter

Section 6: Arrays and building the page with codeLesson 6.2 · ⏱ 9 min

for (let i = 1; i <= 10; i++) counts from 1 to 10. We build the text line by line, then put it on the page in one go with innerHTML.

HTML
<h2>Times table</h2>
<input id="num" type="number" value="2">
<button id="go">Show</button>
<div id="table"></div>
CSS
body { font-family: Tahoma, sans-serif; }
#table { margin-top: 10px; columns: 2; font-size: 18px; }
input { width: 60px; font-size: 18px; }
JavaScript
document.getElementById("go").onclick = function () {
  const n = Number(document.getElementById("num").value);
  let text = "";
  for (let i = 1; i <= 10; i++) {
    text += n + " × " + i + " = " + (n * i) + "<br>";
  }
  document.getElementById("table").innerHTML = text;
};
🖥️ The page in Robo Web:The page in Robo Web
🖥️ What happens on the stage?
We typed 7 and clicked “Show”, and the 7 times table appeared from 7 × 1 to 7 × 10 = 70.
  1. Make the table go up to 12.
  2. Colour the even lines in a different colour (use i % 2).

📌 Another example: the countdown to launch

for counts from 10 to 1, then 🚀.

🌐 The countdown to launch
HTML
<div id="box"></div>
CSS
body { font-family: Tahoma, sans-serif; }
#box { font-size: 22px; }
JavaScript
let text = "";
for (let i = 10; i >= 1; i--) {
  text += i + " ";
}
document.getElementById("box").textContent = text + "🚀";
🖥️ The page in Robo Web:The page in Robo Web
✍️ Practise
Try the example here: copy the code into its tabs, change it, and watch the page change.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Print the numbers from 1 to 5 in the Console.

👀 Show the solution
🌐 Solution
HTML
<p>Console 👇</p>
CSS
body { font-family: Tahoma, sans-serif; }
JavaScript
for (let i = 1; i <= 5; i++) {
  console.log(i);
}
🖥️ The page in Robo Web:The page in Robo Web
🖥️ The Console in Robo Web:
1
2
3
4
5

Result: 1 to 5.

Medium

The even numbers from 2 to 20 on the page.

👀 Show the solution
🌐 Solution
HTML
<p id="out"></p>
CSS
body { font-family: Tahoma, sans-serif; }
JavaScript
let text = "";
for (let i = 2; i <= 20; i += 2) {
  text += i + " ";
}
document.getElementById("out").textContent = text;
🖥️ The page in Robo Web:The page in Robo Web

Result: 2 4 6 ... 20

Hard

A pyramid of stars with 5 lines.

👀 Show the solution
🌐 Solution
HTML
<pre id="out"></pre>
CSS
pre { font-size: 22px; }
JavaScript
let text = "";
for (let i = 1; i <= 5; i++) {
  text += "⭐".repeat(i) + "\n";
}
document.getElementById("out").textContent = text;
🖥️ The page in Robo Web:The page in Robo Web

Result: a pyramid with 5 lines.

Finished the lesson? Mark it so your progress counts.