-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
43 lines (36 loc) · 1.17 KB
/
script.js
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
// Get the result input element by its ID
const result = document.getElementById("result");
// Function to append a value to the result input
function display(value) {
result.value += value;
}
// Function to clear the result input
function clearAll() {
result.value = "";
}
// Function to remove the last character from the result input
function clearLast() {
let currentVal = result.value;
if (currentVal.length > 0) {
result.value = currentVal.slice(0, -1); // Remove the last character from the current text
}
}
// Function to calculate the result of the expression
function calculate() {
let expression = result.value;
// Replace 'x' with '*' and '÷' with '/'
expression = expression.replace(/x/g, "*").replace(/÷/g, "/");
try {
// Evaluate the expression using the math.js library
const expressionResult = math.evaluate(expression);
// Check if the result is a valid number
if (!isNaN(expressionResult)) {
result.value = expressionResult;
} else {
result.value = "invalid";
}
} catch (e) {
// Handle any errors that occur during evaluation
result.value = "Invalid";
}
}