انتقل للمحتوى
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

if: the countdown

Section 3: JavaScript: the actionsLesson 3.4 · ⏱ 8 min
HTML
<h1 id="num">2</h1>
<button id="minus">➖</button>
CSS
body { font-family: Tahoma; text-align: center; }
h1 { font-size: 70px; }
button { font-size: 26px; padding: 8px 24px; }
JavaScript
let count = 2;
const btn = document.getElementById("minus");

btn.onclick = function () {
  if (count > 0) {
    count = count - 1;
  }
  document.getElementById("num").textContent = count;
};
🖥️ The page in Robo Web:The page in Robo Web
🖥️ What happens on the stage?
The number goes down with every click, but it doesn't go below 0.

📌 Another example: pass or fail

The button reads the mark and writes: excellent, pass, or try again.

🌐 Pass or fail
HTML
<input id="grade" type="number" value="75">
<button id="go">The result</button>
<p id="res"></p>
CSS
body { font-family: Tahoma, sans-serif; }
button { font-size: 20px; padding: 8px 20px; }
input { font-size: 20px; width: 80px; }
#res { font-size: 26px; }
JavaScript
document.getElementById("go").onclick = function () {
  const g = Number(document.getElementById("grade").value);
  let r;
  if (g >= 90) r = "Excellent 🌟";
  else if (g >= 50) r = "Pass ✅";
  else r = "Try again 💪";
  document.getElementById("res").textContent = r;
};
🖥️ The page in Robo Web:The page in Robo Web
✍️ Practise
The countdown.
✍️ Practise here in the lessonor open the lab in a new page ↗
✍️ Practise
if in JavaScript.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

The counter doesn't go below zero.

👀 Show the solution
🌐 Solution
HTML
<h1 id="num">0</h1>
<button id="minus">➖</button>
CSS
body { font-family: Tahoma, sans-serif; }
button { font-size: 20px; padding: 8px 20px; }
JavaScript
let count = 0;
document.getElementById("minus").onclick = function () {
  if (count > 0) count--;
  document.getElementById("num").textContent = count;
};
🖥️ The page in Robo Web:The page in Robo Web

Result: it stays 0.

Medium

The counter writes “You made it 🎉” when it reaches 5.

👀 Show the solution
🌐 Solution
HTML
<h1 id="num">0</h1>
<button id="plus">➕</button>
<p id="msg"></p>
CSS
body { font-family: Tahoma, sans-serif; }
button { font-size: 20px; padding: 8px 20px; }
JavaScript
let count = 0;
document.getElementById("plus").onclick = function () {
  count++;
  document.getElementById("num").textContent = count;
  if (count === 5) document.getElementById("msg").textContent = "You made it 🎉";
};
🖥️ The page in Robo Web:The page in Robo Web

Result: You made it 🎉

Hard

Even or odd? (the remainder %).

👀 Show the solution
🌐 Solution
HTML
<input id="n" type="number" value="7">
<button id="go">Check</button>
<p id="res"></p>
CSS
body { font-family: Tahoma, sans-serif; }
button { font-size: 20px; padding: 8px 20px; }
JavaScript
document.getElementById("go").onclick = function () {
  const n = Number(document.getElementById("n").value);
  if (n % 2 === 0) document.getElementById("res").textContent = n + " even";
  else document.getElementById("res").textContent = n + " odd";
};
🖥️ The page in Robo Web:The page in Robo Web

Result: 7 odd, and 8 even.

Finished the lesson? Mark it so your progress counts.