-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
52 lines (48 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
42
43
44
45
46
47
48
49
50
51
52
$(document).ready(function() {
var result = 0;
var eq = [];
var evEq;
var isEval = false;
//AC button
$('#clear').on('click', function() {
$('#numbers').html(0);
eq = [];
});
//number input buttons
if (eq.length <= 16) {
$('.inpBtn').on('click', function() {
if (isEval) {
evEq = undefined;
$('#numbers').html($(this).text());
eq = [$(this).text()];
isEval = false;
} else {
eq.push($(this).text());
$('#numbers').html(eq.join(''));
}
});
} else {
$('#numbers').html("...");
}
//calc operation buttons
$('.calcBtn').on('click', function() {
let val = $(this).text();
const regex = /\D/;
eq.push(val);
if (regex.test(eq[eq.length-2])) {
delete eq[eq.length - 2];
}
$('#numbers').html(eq.join(''));
});
//evaluates the temporary array
$("#equal").on('click', function() {
eq = eq.join('');
evEq = eval(eq);
if (typeof evEq !== "number" || evEq === Infinity) {
$('#numbers').html("ERROR");
} else {
$('#numbers').html(evEq);
}
isEval = true;
});
});