-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (120 loc) · 3.82 KB
/
index.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
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
var op_list = ['<i class="fas fa-plus" aria-hidden="true"></i>',
'<i class="fas fa-minus" aria-hidden="true"></i>',
'<i class="fas fa-times times-btn" aria-hidden="true"></i>',
'<i class="fas fa-divide" aria-hidden="true"></i>']
var num_list = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
var currentQueue = [];
var prevButtonClick = ""
;
function displayNumOnScreen(num){
if (num > 9999999){
$("h1").text("TOO LARGE");
}
else {
$("h1").text(num);
}
}
function clearDisplay(){
displayNumOnScreen(0);
}
function calc(num1, op, num2){
if (op_list.indexOf(op) === 0){
var x = num1 + num2;
return Math.round((x + Number.EPSILON) * 100) / 100;
}
else
if (op_list.indexOf(op) === 1){
var x = num1 - num2;
return Math.round((x + Number.EPSILON) * 100) / 100;
}
else
if (op_list.indexOf(op) === 2){
var x = num1 * num2;
return Math.round((x + Number.EPSILON) * 100) / 100;
}
else
if (op_list.indexOf(op) === 3){
var x = num1/num2;
return Math.round((x + Number.EPSILON) * 100) / 100;
}
else {
return num1 + num2;
}
}
function eval(queue){
while (queue.length > 1) {
queue = [calc(queue[0],queue[1],queue[2])].concat(queue.splice(3,queue.length))
}
return queue[0];
}
$(".num-btn").click(function(event){
if (op_list.includes(prevButtonClick)){
var entry = parseInt(event.currentTarget.innerHTML);
displayNumOnScreen(entry);
prevButtonClick = event.currentTarget.innerHTML;
}
else if (prevButtonClick === "="){
currentQueue = [];
var entry = parseInt(event.currentTarget.innerHTML);
displayNumOnScreen(entry);
prevButtonClick = event.currentTarget.innerHTML;
}
else {
var entry = parseInt(event.currentTarget.innerHTML);
var onScreenNum = parseInt($("h1").text());
onScreenNum = onScreenNum*10 + entry;
displayNumOnScreen(onScreenNum);
prevButtonClick = event.currentTarget.innerHTML;
}
})
$(".clear").click(function(event){
currentQueue = [];
displayNumOnScreen(0);
prevButtonClick = "";
})
$(".operator").click(function(event){
if (prevButtonClick === ""){
prevButtonClick = event.currentTarget.innerHTML;
}
else if (num_list.includes(prevButtonClick)){
var onScreenNum = parseInt($("h1").text());
currentQueue = currentQueue.concat([onScreenNum]);
var rslt = eval(currentQueue);
currentQueue = [rslt, event.currentTarget.innerHTML];
displayNumOnScreen(rslt);
prevButtonClick = event.currentTarget.innerHTML;
}
else if (op_list.includes(prevButtonClick)){
currentQueue = currentQueue.splice(0, currentQueue.length - 1);
currentQueue = currentQueue.concat([event.currentTarget.innerHTML]);
prevButtonClick = event.currentTarget.innerHTML;
}
else if (prevButtonClick === "=") {
currentQueue = currentQueue.concat([event.currentTarget.innerHTML]);
prevButtonClick = event.currentTarget.innerHTML;
}
else {
}
})
$(".result").click(function(event){
if (prevButtonClick === ""){
prevButtonClick = event.currentTarget.innerHTML;
}
else
if (num_list.includes(prevButtonClick)){
var onScreenNum = parseInt($("h1").text());
currentQueue = currentQueue.concat([onScreenNum]);
var rslt = eval(currentQueue);
currentQueue = [rslt];
displayNumOnScreen(rslt);
prevButtonClick = event.currentTarget.innerHTML;
}
else
if (op_list.includes(prevButtonClick)){
prevButtonClick = event.currentTarget.innerHTML;
}
else
if (prevButtonClick === "=") {
prevButtonClick = event.currentTarget.innerHTML;
}
})