forked from akhilack/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
41 lines (41 loc) · 1.12 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
const inputBox = document.getElementById("screen");
const actionInputs = ['(', ')', '+', '-', '/', '*', '%'];
let isActionInput = false;
let inNumberIncludePoint = false;
let invalidExpression = false;
function buttonClick(val) {
if (invalidExpression) {
inputBox.value = '';
invalidExpression = false;
}
if (actionInputs.includes(val)) {
if (!isActionInput) {
inputBox.value += val;
isActionInput = true;
} else {
inputBox.value = inputBox.value.replace(/.$/, val);
}
inNumberIncludePoint = false;
} else if (val === '.') {
if (!inNumberIncludePoint) {
inputBox.value += val;
inNumberIncludePoint = true;
}
isActionInput = false;
} else {
inputBox.value += val;
isActionInput = false;
}
}
function clearDisplay(){
inputBox.value = "";
}
function equalClick() {
try {
inputBox.value = eval(inputBox.value);
}
catch (err) {
invalidExpression = true;
inputBox.value = 'Invalid expression !';
}
}