This repository was archived by the owner on Aug 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (56 loc) · 1.69 KB
/
script.js
File metadata and controls
63 lines (56 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// script.js
let currentInput = "";
function appendValue(value) {
currentInput += value;
document.getElementById("display").value = currentInput;
}
function appendDecimal() {
if (!currentInput.includes(".")) {
currentInput += ".";
document.getElementById("display").value = currentInput;
}
}
function clearDisplay() {
currentInput = "";
document.getElementById("display").value = "";
}
function calculatePercentage() {
if (currentInput) {
currentInput = (parseFloat(currentInput) / 100).toString();
document.getElementById("display").value = currentInput;
}
}
function calculateResult() {
try {
// Replace ^ with ** for exponentiation
let expression = currentInput.replace(/\^/g, '**');
// SECURITY RISK: Using eval() can be dangerous with user-provided input.
currentInput = eval(expression).toString();
document.getElementById("display").value = currentInput;
} catch (e) {
document.getElementById("display").value = "Error";
currentInput = "";
}
}
function compareGreater() {
let values = currentInput.split('>');
if (values.length === 2) {
let left = parseFloat(values[0]);
let right = parseFloat(values[1]);
document.getElementById("display").value = left > right ? "True" : "False";
} else {
currentInput += '>';
document.getElementById("display").value = currentInput;
}
}
function compareLess() {
let values = currentInput.split('<');
if (values.length === 2) {
let left = parseFloat(values[0]);
let right = parseFloat(values[1]);
document.getElementById("display").value = left < right ? "True" : "False";
} else {
currentInput += '<';
document.getElementById("display").value = currentInput;
}
}