-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
112 lines (107 loc) · 3.07 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
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
var calculator = (function() {
//cache DOM
var calc = document.getElementById('calc'); //caching buttons div
var display = document.getElementById('display'); //caching display div
var newCalc = false;
var array = []; //array to track values and operators
//turns array into a string and evaluate it
function evaluate(arr) {
arr = arr.join('');
return eval(arr).toPrecision(10)/1;
};
//pushes value and operator to array
function operator(op) {
var val = parseFloat(display.innerHTML);
if(val) {
array.push(val);
array.push(op);
display.innerHTML = '';
} else if (typeof array[array.length -1] !== 'number' && array.length !== 0) { // change previous operator to current operator
array.pop();
array.push(op);
}
};
// append values to display
function keyNum (val) {
if (newCalc === false) {
newCalc = true;
display.innerHTML = '';
return display.innerHTML += val;
} else {
return display.innerHTML += val;
};
}
//buttons object on calculator
var buttons = {
zero: function() {keyNum(0)},
one: function() {keyNum(1)},
two: function() {keyNum(2)},
three: function() {keyNum(3)},
four: function() {keyNum(4)},
five: function() {keyNum(5)},
six: function() {keyNum(6)},
seven: function() {keyNum(7)},
eight: function() {keyNum(8)},
nine: function() {keyNum(9)},
decimal: function (){
if (display.innerHTML.indexOf('.') === -1 || newCalc === false) {
keyNum('.');
}
},
add: function(){
operator('+');
},
sub: function(){
operator('-');
},
mult: function(){
operator('*');
},
divide: function(){
operator('/');
},
equal: function(){
if(array.length > 0) {
// if there is an operator in last index, push new number to array and evalute
if (parseFloat(display.innerHTML) && typeof array[array.length -1] !== 'number') {
array.push(parseFloat(display.innerHTML));
display.innerHTML = evaluate(array);
array = [];
newCalc = false;
} else if (typeof array[array.length -1] !== 'number') {
array.pop();
display.innerHTML = evaluate(array);
array = [];
newCalc = false;
} else {
display.innerHTML = evaluate(array);
array = [];
newCalc = false;
}
}
},
backspace: function() {
return display.innerHTML = display.innerHTML.slice(0, display.innerHTML.length - 1);
},
clear: function(){
return display.innerHTML = '';
},
"all-clear": function(){
array = [];
newCalc = false;
return display.innerHTML = '';
},
"plus-minus": function() {
if(display.innerHTML.indexOf("-") !== 0) {
return (display.innerHTML = "-" + display.innerHTML);
} else {
return display.innerHTML = display.innerHTML.slice(1);
}
}
}
//event handler
calc.addEventListener('click', function(e) {
e.preventDefault();
buttons[e.target.id]();
});
})();