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:

🖥️ 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.
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:

✍️ Practise
The countdown.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
✍️ Practise
if in JavaScript.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
The counter doesn't go below zero.
👀 Show the solution🙈 Hide the 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:

Result: it stays 0.
Medium
The counter writes “You made it 🎉” when it reaches 5.
👀 Show the solution🙈 Hide the 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:

Result: You made it 🎉
Hard
Even or odd? (the remainder %).
👀 Show the solution🙈 Hide the 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:

Result: 7 odd, and 8 even.

