-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.js
More file actions
36 lines (30 loc) · 781 Bytes
/
Script.js
File metadata and controls
36 lines (30 loc) · 781 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
31
32
33
34
35
36
Script.js
let display = document.getElementById('display');
function clearDisplay() {
display.innerText = '0';
}
function appendNumber(number) {
if (display.innerText === '0') {
display.innerText = number;
} else {
display.innerText += number;
}
}
function appendOperator(operator) {
const lastChar = display.innerText.slice(-1);
if ('+-*/'.includes(lastChar)) {
display.innerText = display.innerText.slice(0, -1) + operator;
} else {
display.innerText += operator;
}
}
function deleteLast() {
display.innerText = display.innerText.slice(0, -1) || '0';
}
function calculate() {
try {
display.innerText = eval(display.innerText) || '0';
} catch {
display.innerText = 'Error';
}
}