-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (61 loc) · 1.49 KB
/
app.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
$(document).ready(function() {
var num = '',
newNum = '',
temp = '',
total = $('#total-display');
//keeps the inputs at 15 characters
var testNumLength = function(number) {
if (number.length > 15) {
total.text(num.substr(number.length - 15, 15));
}
};
total.text('0');
$('#button-clear').click(function() {
num = '';
newNum = '';
temp = '';
total.text('Clear');
});
$('#button-decimal').click(function() {
//adds a 0 (zero) if decimal is the first button clicked
if (num.length === 0) {
num += '0';
}
var numOfDecs = 0;
//checks if a decimal has already been added and if it has then cannot add another
for (i = 0; i < num.length; i++) {
if (num[i] === '.') {
numOfDecs++;
}
}
if (numOfDecs === 0) {
num += '.';
}
total.text(num);
testNumLength(num);
});
$('.button-number').click(function() {
num += $(this).text();
total.text(num);
testNumLength(num);
});
$('.button-operator').click(function() {
temp += num;
temp += $(this).text();
num = newNum;
num = '';
total.text('0');
});
$('#button-equal').click(function() {
temp += num;
//replaces all the x and ÷ characters to * and / for math calculations
temp = temp.replace(/x/g, '*').replace(/÷/g, '/');
var grandTotal = eval(temp);
total.text(grandTotal);
testNumLength(grandTotal);
num = '';
newNum = '';
temp = '';
grandTotal = '';
});
});