-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathscript.js
More file actions
30 lines (21 loc) · 753 Bytes
/
script.js
File metadata and controls
30 lines (21 loc) · 753 Bytes
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
var input = document.getElementById("display");
//script to detect if enter button is clicked to evaluate the result
input.addEventListener("keyup", function(event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode == 13) {
// Trigger the button element with a click
document.getElementById("equal").click();
}
});
// Function to calculate the result
function calculate(operation) {
let result;
try {
result = eval(operation);
} catch (error) {}
if ( result === 0 || isFinite(result) && Boolean(result) ) {
document.getElementById('display').value = result;
} else {
document.getElementById('display').value = 'Invalid operation';
}
}