-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
159 lines (141 loc) · 4.99 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
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
let calcH1 = document.querySelector('#calcText');
let calcString = [];
let clearButton = document.querySelector('#btnClear');
let backSpaceButton = document.querySelector('#btnBackspace');
let equalsButton = document.querySelector('#btnEquals');
const numButtons = [
document.querySelector('#btn0'),
document.querySelector('#btn1'),
document.querySelector('#btn2'),
document.querySelector('#btn3'),
document.querySelector('#btn4'),
document.querySelector('#btn5'),
document.querySelector('#btn6'),
document.querySelector('#btn7'),
document.querySelector('#btn8'),
document.querySelector('#btn9'),
document.querySelector('#btnDecimal')
];
for (let i = 0; i < numButtons.length; i++) {
numButtons[i].addEventListener('click', inputNumber);
}
const operatorButtons = [
document.querySelector('#btnPlus'),
document.querySelector('#btnMinus'),
document.querySelector('#btnMultiply'),
document.querySelector('#btnDivide')
];
for (let i = 0; i < operatorButtons.length; i++) {
operatorButtons[i].addEventListener('click', inputNumber);
}
add = (num1, num2) => Number(num1) + Number(num2);
minus = (num1, num2) => Number(num1) - Number(num2);
multiply = (num1, num2) => Number(num1) * Number(num2);
divide = (num1, num2) => Number(num1) / Number(num2);
function inputNumber() {
let buttonContent = this.textContent;
//Stops first entry being operator
if (calcString.length < 1 && isOperator(buttonContent) == true) {
return;
}
//If button pressed is an Operator AND so is the last in the Array
if (isOperator(buttonContent) == true && isOperator(calcString[calcString.length - 1]) == true) {
//Changes operator via pop
calcString.pop();
calcString.push(buttonContent);
} else if (isOperator(buttonContent) == true) {
//Add operator to new array item
calcString.push(buttonContent);
} else if (isDecimal(buttonContent)) {
if(isOperator(calcString[calcString.length - 1]) || calcString.length == 0 || calcString[calcString.length - 1].includes('.')){
return;
} else{
calcString[calcString.length - 1] = calcString[calcString.length - 1] + buttonContent;
}
} else {
if (calcString.length == 0) {
calcString[0] = buttonContent;
} else {
//Checks if current array entry is a operator or number
if (isOperator(calcString[calcString.length - 1]) == true) {
//If it is an operator, create new array item
calcString.push(buttonContent);
} else {
//if not then to existing array item
calcString[calcString.length - 1] = calcString[calcString.length - 1] + buttonContent;
}
}
}
//Update display header
calcH1.textContent = calcString.join('');
}
function isOperator(clickedButton) {
if (clickedButton == '+' || clickedButton == '-' || clickedButton == '÷' || clickedButton == 'x') {
return true;
} else {
return false;
}
}
function isDecimal(clickedButton) {
if (clickedButton == '.') {
return true;
} else {
return false;
}
}
clearButton.addEventListener('click', function() {
calcString = [];
calcH1.textContent = '0';
});
backSpaceButton.addEventListener('click', function() {
console.log('hello');
if (isOperator(calcString[calcString.length - 1]) == true) {
calcString.pop();
} else if (typeof calcString[calcString.length - 1] == 'string') {
let original = calcString[calcString.length - 1];
calcString[calcString.length - 1] = original.substring(0, (original.length - 1));
}
//Update display header
if (calcString[0] == '') {
calcH1.textContent = '0';
} else {
calcH1.textContent = calcString.join('');
}
});
equalsButton.addEventListener('click', function() {
if (calcString.length < 3) {
return;
}
let result;
let num1 = calcString.shift();;
let operator = calcString.shift();;
let num2 = calcString.shift();;
if (isOperator(calcString[calcString.length - 1]) == true) {
calcString.pop();
}
//Shortens array with each loop
//So long as there is an operator it will continue to loop
while (isOperator(operator)) {
if (operator == '+') {
result = add(num1, num2);
} else if (operator == '-') {
result = minus(num1, num2);
} else if (operator == 'x') {
result = multiply(num1, num2);
} else if (operator == '÷') {
if (num2 == 0) {
//Stops if divided by 0
calcH1.textContent = 'No Dividing by 0!'
return;
}
result = divide(num1, num2);
} else {
//(Bug catching)
console.log('ERROR: OPERATOR NOT FOUND')
}
num1 = result;
operator = calcString.shift();
num2 = calcString.shift();
}
calcH1.textContent = result.toFixed(2);
})