-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.html
162 lines (144 loc) · 5.52 KB
/
Calculator.html
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=\, initial-scale=1.0">
<title>Calculator</title>
<link rel="stylesheet" href="calculator.css">
</head>
<script>
let totalString;
let queueData = [];
let input = 0;
// to disable keyboard input
document.onkeydown = function (e) {
return false;
}
function getDigit(x) {
input = String(input) + x;
totalString = document.getElementById("inputDisplay").value += x;
}
function getOperator(x) {
if (input !== 0) {
input = parseFloat(input);
addToQueue(input);
addToQueue(x);
console.log('queue', queueData);
totalString = document.getElementById("inputDisplay").value += x;
input = 0;
}
}
function addToQueue(input) {
// alert(input)
queueData.push(input);
}
function clearDigit() {
queueData = [];
input = 0;
document.getElementById("inputDisplay").value = 0;
}
function deleteDigit() {
totalString = totalString.substring(0, totalString.length - 1);
console.log('total string', totalString);
input = String(input).substring(0, String(input).length - 1);
document.getElementById("inputDisplay").value = totalString;
}
//function to remove NAN values from the array.but not called.
function filter_array(test_array) {
let index = -1,
arr_length = test_array ? test_array.length : 0,
resIndex = -1,
result = [];
while (++index < arr_length) {
let value = test_array[index];
if (value) {
result[++resIndex] = value;
}
}
return result;
};
function calculate(value) {
if (input !== 0) {
input = parseFloat(input);
addToQueue(input);
}
var answer = value[0];
var dividedByZero = 0;
for (var i = 2; i < value.length; i = i + 2) {
switch (queueData[i - 1]) {
case '+':
answer += value[i];
break;
case '-':
answer -= value[i];
break;
case '/':
if (value[i] === 0)
dividedByZero = 1;
else
answer = answer / value[i];
break;
case '*':
answer = answer * value[i];
break;
case '%':
answer = answer % value[i];
break;
}
}
// answer = answer.toFixed(10);
answer = parseFloat(answer);
if (dividedByZero === 1) {
clearDigit();
document.getElementById("inputDisplay").value = "ERROR";
}
else {
document.getElementById("inputDisplay").value = answer;
input = answer;
queueData = [];
}
}
</script>
<body>
<div class="center">
<div class="main-bg">
<div class="button-row">
<div>
<input type="text" id="inputDisplay" pattern="[0-9 + - / *]">
</div>
<div>
<button class="dark-gray" onclick="clearDigit()">C</button>
<button class="dark-gray" onclick="deleteDigit()">DEL</button>
<button class="dark-gray" onclick="getOperator('%')">%</button>
<button class="orange-peel font-white" onclick="getOperator('/')">/</button>
</div>
<div>
<button class="gray20-color font-white" onclick="getDigit(7)">7</button>
<button class="gray20-color font-white" onclick="getDigit(8)">8</button>
<button class="gray20-color font-white" onclick="getDigit(9)">9</button>
<button class="orange-peel font-white" onclick="getOperator('*')">*</button>
</div>
<div>
<button class="gray20-color font-white" onclick="getDigit(4)">4</button>
<button class="gray20-color font-white" onclick="getDigit(5)">5</button>
<button class="gray20-color font-white" onclick="getDigit(6)">6</button>
<button class="orange-peel font-white" onclick="getOperator('-')">-</button>
</div>
<div>
<button class="gray20-color font-white" onclick="getDigit(1)">1</button>
<button class="gray20-color font-white" onclick="getDigit(2)">2</button>
<button class="gray20-color font-white" onclick="getDigit(3)">3</button>
<button class="orange-peel font-white" onclick="getOperator('+')">+</button>
</div>
<div>
<button class="gray20-color font-white"
style="width: 175px; border-radius: 50px; text-align: left; padding-left: 20px;"
onclick="getDigit(0)">0</button>
<button class="gray20-color font-white" onclick="getDigit('.')">.</button>
<button class="orange-peel font-white" onclick="calculate(queueData)">=</button>
</div>
</div>
</div>
</div>
</body>
</html>