Skip to content
Open
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
44 changes: 44 additions & 0 deletions demos/calculator-app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const display = document.getElementById("display");
const buttons = document.querySelectorAll("button");

buttons.forEach(button => {
button.addEventListener("click", () => handleInput(button.innerText));
});

function handleInput(value) {
if (value === "C") {
display.value = "";
} else if (value === "⌫") {
display.value = display.value.slice(0, -1);
} else if (value === "=") {
calculate();
} else {
display.value += value;
}
}

function calculate() {
try {
const result = display.value
.replace(/×/g, "*")
.replace(/÷/g, "/")
.replace(/−/g, "-");

display.value = eval(result);
} catch {
display.value = "Error";
}
}

/* Keyboard Support */
document.addEventListener("keydown", (e) => {
if ((e.key >= 0 && e.key <= 9) || "+-*/.".includes(e.key)) {
display.value += e.key;
} else if (e.key === "Enter") {
calculate();
} else if (e.key === "Backspace") {
display.value = display.value.slice(0, -1);
} else if (e.key === "Escape") {
display.value = "";
}
});
41 changes: 41 additions & 0 deletions demos/calculator-app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Calculator UI App</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>

<div class="calculator">
<input type="text" id="display" readonly />

<div class="buttons">
<button class="clear">C</button>
<button class="delete">⌫</button>
<button class="operator">÷</button>
<button class="operator">×</button>

<button>7</button>
<button>8</button>
<button>9</button>
<button class="operator">−</button>

<button>4</button>
<button>5</button>
<button>6</button>
<button class="operator">+</button>

<button>1</button>
<button>2</button>
<button>3</button>
<button class="equal">=</button>

<button class="zero">0</button>
<button>.</button>
</div>
</div>

<script src="app.js"></script>
</body>
</html>
76 changes: 76 additions & 0 deletions demos/calculator-app/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Verdana, Tahoma, sans-serif;
}

body {
height: 100vh;
background: linear-gradient(135deg, #764ba2, #667eea);
display: flex;
justify-content: center;
align-items: center;
}

.calculator {
width: 320px;
background: #222;
border-radius: 10px;
padding: 15px;
}

#display {
width: 100%;
height: 60px;
background: #000;
color: #0f0;
font-size: 2rem;
text-align: right;
padding: 10px;
border: none;
margin-bottom: 10px;
}

.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}

button {
padding: 15px;
font-size: 1.2rem;
border: none;
border-radius: 5px;
cursor: pointer;
}

button:hover {
opacity: 0.8;
}

.operator {
background: #ff9f43;
}

.equal {
background: #1dd1a1;
grid-row: span 2;
}

.clear {
background: #ee5253;
}

.delete {
background: #576574;
}

.zero {
grid-column: span 2;
}

button:not(.operator):not(.equal):not(.clear):not(.delete) {
background: #c8d6e5;
}