-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
66 lines (59 loc) · 1.58 KB
/
main.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
let displayArray = [];
let mathButton = [];
let mathArray = [];
let firstSet = [];
let mathGroup = [];
window.onload = function() {
document.getElementById("calcText").value = 0;
};
function isNumber(displayArray) {
return !isNaN(parseFloat(displayArray)) && isFinite(displayArray);
}
function getReady() {
firstSet = displayArray.filter(isNumber);
displayArray = [];
mathArray = firstSet.join("");
mathGroup.push(mathArray);
mathArray = [];
mathGroup.map(function(item) {
return parseInt(item);
});
}
function updateDisplay(id) {
var numButton = id.innerHTML;
displayArray.push(numButton);
document.getElementById("calcText").value = displayArray.join("");
}
function mathDisplay(id) {
mathButton = id.innerHTML;
displayArray.push(mathButton);
document.getElementById("calcText").value = displayArray.join("");
getReady();
}
function clearDisplay() {
document.getElementById("calcText").value = 0;
displayArray = [];
}
$("#equal").on("click", function() {
getReady();
if (mathButton === "+") {
var sum = mathGroup.reduce(function(a, b) {
return Number(a) + Number(b);
});
} else if (mathButton === "-") {
var sum = mathGroup.reduce(function(a, b) {
return Number(a) - Number(b);
});
} else if (mathButton === "*") {
var sum = mathGroup.reduce(function(a, b) {
return Number(a) * Number(b);
});
} else if (mathButton === "/") {
var sum = mathGroup.reduce(function(a, b) {
return Number(a) / Number(b);
});
}
document.getElementById("calcText").value = sum;
displayArray = [];
mathGroup = [];
});