๐ ๏ธ Section project: the calculator
Section 7: Events and formsLesson 7.3 ยท โฑ 13 min
๐ฏ Project goal
A calculator: two numbers and four buttons, and the answer appears under them. And it doesn't accept dividing by zero.
- Every button carries its operation in
data-op, so one function is enough for the four buttons. Number(โฆ)turns what was typed in the box into a number, otherwise 12 + 3 becomes โ123โ.else if (b === 0)protects the program before dividing.
HTML
<div class="calc">
<h2>๐งฎ Calculator</h2>
<input id="a" type="number" value="12">
<input id="b" type="number" value="3">
<div class="ops">
<button data-op="+">+</button>
<button data-op="-">โ</button>
<button data-op="*">ร</button>
<button data-op="/">รท</button>
</div>
<p id="result">?</p>
</div>
CSS
body { font-family: Tahoma, sans-serif; }
.calc { width: 260px; margin: 10px auto; padding: 16px; border-radius: 18px; background: #1e1e2e; color: white; text-align: center; }
input { width: 90px; font-size: 20px; margin: 4px; text-align: center; }
.ops button { width: 50px; height: 44px; font-size: 22px; margin: 4px; border: none; border-radius: 12px; background: #ff7a00; color: white; }
#result { font-size: 28px; font-weight: bold; }
JavaScript
document.querySelectorAll(".ops button").forEach(function (btn) {
btn.onclick = function () {
const a = Number(document.getElementById("a").value);
const b = Number(document.getElementById("b").value);
const op = btn.dataset.op;
let r;
if (op === "+") r = a + b;
else if (op === "-") r = a - b;
else if (op === "*") r = a * b;
else if (b === 0) r = "You can't divide by zero";
else r = a / b;
document.getElementById("result").textContent = r;
};
});
๐ฅ๏ธ The page in Robo Web:

๐ฅ๏ธ What happens on the stage?
We tried it in the lab: 12 รท 3 = 4, then 12 รท 0 and โYou can't divide by zeroโ appeared, then 12 ร 3 = 36 as in the picture.
- Add a power button ^ (
a ** b). - Add a โClearโ button that puts the two boxes back to zero.
๐ Another example: the pocket money calculator
Pocket money a week ร the number of weeks, and the answer appears as you type.
HTML
<label>Pocket money a week: <input id="w" type="number" value="50"></label><br>
<label>Number of weeks: <input id="n" type="number" value="4"></label>
<p id="r"></p>
CSS
body { font-family: Tahoma, sans-serif; }
input { width: 80px; font-size: 20px; }
button { font-size: 20px; padding: 6px 14px; }
#r { font-size: 26px; }
JavaScript
function calc() {
const w = Number(document.getElementById("w").value);
const n = Number(document.getElementById("n").value);
document.getElementById("r").textContent = "Total: " + (w * n) + " pounds";
}
document.getElementById("w").oninput = calc;
document.getElementById("n").oninput = calc;
calc();
๐ฅ๏ธ 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 lessonโ Close the labor open the lab in a new page โ
๐ฏ Challenge yourself
Easy
A โ+โ button adds the two numbers.
๐ Show the solution๐ Hide the solution
HTML
<input id="a" type="number" value="12">
<input id="b" type="number" value="4">
<button id="plus">+</button>
<p id="r"></p>
CSS
body { font-family: Tahoma, sans-serif; }
input { width: 80px; font-size: 20px; }
button { font-size: 20px; padding: 6px 14px; }
#r { font-size: 26px; }
JavaScript
document.getElementById("plus").onclick = function () {
const a = Number(document.getElementById("a").value);
const b = Number(document.getElementById("b").value);
document.getElementById("r").textContent = a + b;
};
๐ฅ๏ธ The page in Robo Web:

Result: 16
Medium
A โ^โ button for the power: a ** b.
๐ Show the solution๐ Hide the solution
HTML
<input id="a" type="number" value="2">
<input id="b" type="number" value="5">
<button id="pow">^</button>
<p id="r"></p>
CSS
body { font-family: Tahoma, sans-serif; }
input { width: 80px; font-size: 20px; }
button { font-size: 20px; padding: 6px 14px; }
#r { font-size: 26px; }
JavaScript
document.getElementById("pow").onclick = function () {
const a = Number(document.getElementById("a").value);
const b = Number(document.getElementById("b").value);
document.getElementById("r").textContent = a ** b;
};
๐ฅ๏ธ The page in Robo Web:

Result: 2 to the power 5 = 32
Hard
If one of the boxes is empty, โType two numbersโ appears.
๐ Show the solution๐ Hide the solution
HTML
<input id="a" type="number" value="12">
<input id="b" type="number" value="4">
<button id="plus">+</button>
<p id="r"></p>
CSS
body { font-family: Tahoma, sans-serif; }
input { width: 80px; font-size: 20px; }
button { font-size: 20px; padding: 6px 14px; }
#r { font-size: 26px; }
JavaScript
document.getElementById("plus").onclick = function () {
const a = document.getElementById("a").value;
const b = document.getElementById("b").value;
if (a === "" || b === "") {
document.getElementById("r").textContent = "Type two numbers";
return;
}
document.getElementById("r").textContent = Number(a) + Number(b);
};
๐ฅ๏ธ The page in Robo Web:

Result: 16, then โType two numbersโ.

