-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
238 lines (206 loc) · 5.98 KB
/
calculator.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
$(document).ready(function () {
var $calculator = $('#calculator');
// CREATE STATIC HTML ELEMENTS-----------------
// Calculator Screen
var $screen = $('<div></div>');
var $screenText = $('<p>0</p>');
// Auxillary Buttons
var $aux = $('<div></div>').attr('class', 'aux');
var $clear = $('<button id="clear">c</button>');
var $sign = $('<button id="sign">+/-</button>');
var $percent = $('<button id="percent">%</button>');
// Operator Buttons
var $operators = $('<div></div>').attr('class', 'operators');
var $division = $('<button id="division">÷</button>');
var $multiplication = $('<button id="multiplication">x</button>');
var $subtraction = $('<button id="subtraction">-</button>');
var $addition = $('<button id="addition">+</button>');
var $equals = $('<button id="equals">=</button>');
// Number Buttons
var $numbers = $('<div></div>').attr('class', 'numbers');
var $seven = $('<button id="seven">7</button>');
var $eight = $('<button id="eight">8</button>');
var $nine = $('<button id="nine">9</button>');
var $four = $('<button id="four">4</button>');
var $five = $('<button id="five">5</button>');
var $six = $('<button id="six">6</button>');
var $one = $('<button id="one">1</button>');
var $two = $('<button id="two">2</button>');
var $three = $('<button id="three">3</button>');
var $zero = $('<button id="zero">0</button>');
var $decimal = $('<button id="decimal">.</button>');
// Class Attributes
var $allAux = [$clear, $sign, $percent];
var $allOperators = [$division, $multiplication, $subtraction, $addition, $equals];
var $allNumbers = [
$seven,
$eight,
$nine,
$four,
$five,
$six,
$one,
$two,
$three,
$zero,
$decimal
];
var assignClass = function ($collection, $class) {
$collection.forEach(function ($e) {
$e.addClass($class);
});
};
assignClass([$screen, $screenText], 'screen');
assignClass([$aux, $operators, $numbers], 'container');
assignClass($allAux, 'auxillary');
assignClass($allOperators, 'operator');
assignClass($allNumbers, 'number');
// FUNCTIONS TO HANDLE EVENT LISTENERS-------------
var reset = function () {
window.total = 0;
$.each(operations, function (k, op) {
if (op) {
operations[k] = false;
}
});
window.pending = false;
window.currentNum = 0;
window.first = true;
window.equalsChain = false;
rmClass();
$screenText.text('0');
};
var rmClass = function () {
$('.op-clicked').removeClass('op-clicked');
};
var toNum = function () {
return Number($screenText.text());
};
var storedOp = function () {
var operation = null;
$.each(operations, function (op, stored) {
if (stored) {
operation = calculate[op];
}
});
return operation;
};
var handleOperatorClick = function ($btn) {
var stored;
if ($btn.attr('id') !== 'equals') {
rmClass();
$btn.addClass('op-clicked');
if (window.equalsChain) {
window.equalsChain = false;
}
if (window.first) {
window.first = false;
total = toNum();
} else {
stored = storedOp();
$.each(operations, function (k, op) {
if (op) {
total = stored(total, Number($screenText.text()));
current = toNum();
$screenText.text(total.toString());
operations[k] = false;
}
});
}
operations[$btn.attr('id')] = true;
}
pending = true;
};
var handleEqualsClick = function () {
var stored = storedOp();
var $screenNum = toNum();
if (!window.equalsChain) {
if (stored) {
total = stored(total, $screenNum);
currentNum = $screenNum;
window.equalsChain = true;
$screenText.text(total.toString());
return;
}
} else {
if (stored) {
total = stored($screenNum, currentNum);
$screenText.text(total.toString());
}
}
rmClass();
};
var handleNumberClick = function ($num, $limit) {
var clear = function () {
$screenText.text('');
}
if ($('.op-clicked').length > 0) {
rmClass();
clear();
}
$.each(operations, function (k, op) {
if (op && pending) {
pending = false;
clear();
}
});
if ($num !== '.') {
if ($screenText.text().length < $limit) {
if ($screenText.text() === '0') {
clear();
}
$screenText.append($num);
}
}
};
var handleSignClick = function () {
var $number = $screenText.text();
if (!$number.match(/-/)) {
$screenText.prepend('-');
} else {
$screenText.text($number.substring(1));
}
};
var handleDecimalClick = function () {
var $number = $screenText.text();
if ($number.indexOf('.') === -1 && $number.length <= 8) {
$screenText.append('.');
}
};
// EVENT LISTENERS--------------------------
$(document).on('click', '.operator', function () {
handleOperatorClick($(this));
});
$(document).on('click', '.number', function () {
var $num = $(this).text();
var $limit = $num[0] === '-' ? 9 : 8;
handleNumberClick($num, $limit);
});
$(document).on('click', '#clear', function () {
reset();
});
$(document).on('click', '#sign', function () {
handleSignClick();
});
$(document).on('click', '#decimal', function () {
handleDecimalClick();
});
$(document).on('click', '#percent', function () {
var $num = Number($screenText.text());
$screenText.text($num / 100);
});
$(document).on('click', '#equals', function () {
handleEqualsClick();
});
// ADD ELEMENTS TO THE DOM-------------------
var appendElements = function ($parent, $children) {
$children.forEach(function ($e) {
$parent.append($e);
});
$parent.appendTo($calculator);
};
$screen.append($screenText).appendTo($calculator);
appendElements($aux, $allAux);
appendElements($operators, $allOperators);
appendElements($numbers, $allNumbers);
});