ุงู†ุชู‚ู„ ู„ู„ู…ุญุชูˆู‰
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

๐Ÿ› ๏ธ 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: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.
  1. Add a power button ^ (a ** b).
  2. 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.

๐ŸŒ The pocket money calculator
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: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

A โ€œ+โ€ button adds the two numbers.

๐Ÿ‘€ Show the solution
๐ŸŒ 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:The page in Robo Web

Result: 16

Medium

A โ€œ^โ€ button for the power: a ** b.

๐Ÿ‘€ Show the solution
๐ŸŒ 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: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
๐ŸŒ 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:The page in Robo Web

Result: 16, then โ€œType two numbersโ€.

Finished the lesson? Mark it so your progress counts.