-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Added calculator app #1110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Added calculator app #1110
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Calculator</title> | ||
| <link rel="stylesheet" href="style.css"> | ||
| </head> | ||
| <body> | ||
|
|
||
| <div class="calculator"> | ||
| <input type="text" id="display" disabled /> | ||
|
|
||
| <div class="buttons"> | ||
| <button onclick="clearAll()">AC</button> | ||
| <button onclick="clearLast()">C</button> | ||
| <button onclick="press('/')">/</button> | ||
|
|
||
| <button onclick="press('7')">7</button> | ||
| <button onclick="press('8')">8</button> | ||
| <button onclick="press('9')">9</button> | ||
| <button onclick="press('*')">*</button> | ||
|
|
||
| <button onclick="press('4')">4</button> | ||
| <button onclick="press('5')">5</button> | ||
| <button onclick="press('6')">6</button> | ||
| <button onclick="press('-')">-</button> | ||
|
|
||
| <button onclick="press('1')">1</button> | ||
| <button onclick="press('2')">2</button> | ||
| <button onclick="press('3')">3</button> | ||
| <button onclick="press('+')">+</button> | ||
|
|
||
| <button onclick="press('0')">0</button> | ||
| <button onclick="calculate()">=</button> | ||
| </div> | ||
| </div> | ||
|
|
||
| <script src="script.js"></script> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| let display = document.getElementById("display"); | ||
|
|
||
| function press(val) { | ||
| display.value += val; | ||
| } | ||
|
|
||
| function calculate() { | ||
| try { | ||
| display.value = eval(display.value); // (ok for practice) | ||
| } catch { | ||
|
Comment on lines
+3
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify there is no remaining use of eval in JS files.
rg -nP --type=js '\beval\s*\('Repository: florinpop17/app-ideas Length of output: 153 Remove
🔒 Safer approach (no dynamic code execution)+function evaluateExpression(expression) {
+ const sanitized = expression.replace(/\s+/g, "");
+ const tokens = sanitized.match(/\d+(\.\d+)?|[()+\-*/]/g);
+ if (!tokens || tokens.join("") !== sanitized) throw new Error("Invalid expression");
+
+ const prec = { "+": 1, "-": 1, "*": 2, "/": 2 };
+ const output = [];
+ const ops = [];
+
+ for (const t of tokens) {
+ if (/^\d/.test(t)) output.push(Number(t));
+ else if (t in prec) {
+ while (ops.length && ops[ops.length - 1] in prec && prec[ops[ops.length - 1]] >= prec[t]) {
+ output.push(ops.pop());
+ }
+ ops.push(t);
+ } else if (t === "(") ops.push(t);
+ else if (t === ")") {
+ while (ops.length && ops[ops.length - 1] !== "(") output.push(ops.pop());
+ if (ops.pop() !== "(") throw new Error("Mismatched parentheses");
+ }
+ }
+ while (ops.length) {
+ const op = ops.pop();
+ if (op === "(") throw new Error("Mismatched parentheses");
+ output.push(op);
+ }
+
+ const stack = [];
+ for (const t of output) {
+ if (typeof t === "number") stack.push(t);
+ else {
+ const b = stack.pop();
+ const a = stack.pop();
+ if (a === undefined || b === undefined) throw new Error("Invalid expression");
+ stack.push(t === "+" ? a + b : t === "-" ? a - b : t === "*" ? a * b : a / b);
+ }
+ }
+ if (stack.length !== 1) throw new Error("Invalid expression");
+ return stack[0];
+}
+
function calculate() {
try {
- display.value = eval(display.value); // (ok for practice)
+ display.value = String(evaluateExpression(display.value));
} catch {
display.value = "ERR";
}
}🧰 Tools🪛 Biome (2.4.10)[error] 9-9: eval() exposes to security risks and performance issues. (lint/security/noGlobalEval) 🤖 Prompt for AI Agents |
||
| display.value = "ERR"; | ||
| } | ||
| } | ||
|
|
||
| function clearAll() { | ||
| display.value = ""; | ||
| } | ||
|
|
||
| function clearLast() { | ||
| display.value = display.value.slice(0, -1); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| body { | ||
| font-family: Arial; | ||
| text-align: center; | ||
| } | ||
|
|
||
| .calculator { | ||
| width: 200px; | ||
| margin: auto; | ||
| } | ||
|
|
||
| button { | ||
| width: 40px; | ||
| height: 40px; | ||
| margin: 2px; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer
readonlyoverdisabledfor the display field.disabledprevents focus/selection and is less accessible for a calculator display.✅ Suggested HTML tweak
📝 Committable suggestion
🤖 Prompt for AI Agents