Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tools/calculator.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
margin: 2rem auto;
background: white;
border-radius: 12px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
overflow: hidden;
}

Expand Down Expand Up @@ -50,4 +50,4 @@

.calc-buttons button:last-child:hover {
background: #059669;
}
}
80 changes: 40 additions & 40 deletions tools/calculator.html
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<link rel="stylesheet" href="../css/style.css">
<link rel="stylesheet" href="calculator.css">
</head>
<body>
<header>
<h1>Calculator</h1>
<a href="../index.html" class="btn">← Back</a>
</header>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Calculator</title>
<link rel="stylesheet" href="../css/style.css" />
<link rel="stylesheet" href="calculator.css" />
</head>
<body>
<header>
<h1>Calculator</h1>
<a href="../index.html" class="btn">← Back</a>
</header>

<main class="calc-container">
<div class="calc-screen" id="screen">0</div>
<div class="calc-buttons">
<button>C</button>
<button>←</button>
<button>%</button>
<button>/</button>
<main class="calc-container">
<div class="calc-screen" id="screen">0</div>
<div class="calc-buttons">
<button>C</button>
<button>←</button>
<button>%</button>
<button>/</button>

<button>7</button>
<button>8</button>
<button>9</button>
<button>*</button>
<button>7</button>
<button>8</button>
<button>9</button>
<button>*</button>

<button>4</button>
<button>5</button>
<button>6</button>
<button>-</button>
<button>4</button>
<button>5</button>
<button>6</button>
<button>-</button>

<button>1</button>
<button>2</button>
<button>3</button>
<button>+</button>
<button>1</button>
<button>2</button>
<button>3</button>
<button>+</button>

<button>0</button>
<button>.</button>
<button>=</button>
</div>
</main>
<button>0</button>
<button>.</button>
<button>=</button>
</div>
</main>

<script src="calculator.js"></script>
</body>
</html>
<script src="calculator.js"></script>
</body>
</html>
13 changes: 5 additions & 8 deletions tools/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@ const buttons = document.querySelectorAll(".calc-buttons button");

let current = "";

buttons.forEach(btn => {
buttons.forEach((btn) => {
btn.addEventListener("click", () => {
const value = btn.textContent;

if (value === "C") {
current = "";
screen.textContent = "0";
}
else if (value === "←") {
} else if (value === "←") {
current = current.slice(0, -1);
screen.textContent = current || "0";
}
else if (value === "=") {
} else if (value === "=") {
try {
current = eval(current).toString();
screen.textContent = current;
} catch {
screen.textContent = "Error";
current = "";
}
}
else {
} else {
current += value;
screen.textContent = current;
}
});
});
});