-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
184 lines (168 loc) · 5.65 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//Global Variables.
let displayValue = 0 //Return this to update the display in further functions.
const buttons = document.querySelectorAll(`button`);
let firstOperand = null;
let firstOperator = null;
let secondOperand = null;
let secondOperator = null;
let result = null;
//Function to update the display.
function updateDisplay() {
const display = document.getElementById(`display`);
display.innerText = displayValue;
if (displayValue.length > 9) {
display.innerText = displayValue.substring(0, 9);
}
}
updateDisplay();
//function to add eventListeners to all buttons
function addEventButtons() {
for (let i = 0; i < buttons.length; i++) {
buttons[i].addEventListener(`click`, function() {
if (buttons[i].classList.contains(`operand`)) {
inputOperand(buttons[i].value);
updateDisplay();
} else if (buttons[i].classList.contains(`operator`)) {
inputOperator(buttons[i].value);
updateDisplay();
} else if (buttons[i].classList.contains(`clear`)) {
clearDisplay();
updateDisplay();
} else if (buttons[i].classList.contains(`equals`)) {
equals()
updateDisplay();
} else if (buttons[i].classList.contains(`decimal`)) {
inputDecimals(buttons[i].value);
updateDisplay();
} else if (buttons[i].classList.contains(`sign`)) {
inputSign(displayValue);
updateDisplay();
} else if (buttons[i].classList.contains(`percent`)) {
inputPercent(displayValue);
updateDisplay();
}
});
}
}
addEventButtons();
//Function for inputing operands.
function inputOperand(operand){
if (firstOperator === null) {
if(displayValue === `0` || displayValue === 0) {
displayValue = operand;
} else if (displayValue === firstOperand){
displayValue = operand;
} else {
displayValue += operand;
}
} else {
if(displayValue === firstOperand) {
displayValue = operand;
} else {
displayValue += operand;
}
}
};
//Function for inputing operator and getting results
function inputOperator(operator) {
if (firstOperator != null && secondOperator === null) {
//This if handles the input of a second operator.
secondOperator = operator;
secondOperand = displayValue;
result = operate(Number(firstOperand), Number(secondOperand), firstOperator);
displayValue = accuratelyRound(result, 15).toString();
firstOperand = displayValue;
result = null;
} else if (firstOperand != null && secondOperator != null) {
//Handles a 3rd operator.
secondOperand = displayValue;
result = operate(Number(firstOperand), Number(secondOperand), secondOperator);
secondOperator = operator;
displayValue = accuratelyRound(result, 15).toString();
firstOperand = displayValue;
result = null;
} else {
//Handles 2nd Operator
firstOperator = operator;
firstOperand = displayValue;
}
}
//Function for equals and getting results.
function equals () {
//This if stops undefined showing if equals entered before an operator
if (firstOperator === null) {
displayValue = displayValue;
} else if (secondOperator != null) { //This else if handles the final result.
secondOperand = displayValue;
result = operate(Number(firstOperand), Number(secondOperand), secondOperator);
if (result === `don't / 0`) {
displayValue = `don't / 0`;
} else {
displayValue = accuratelyRound(result, 15).toString();
firstOperand = displayValue;
secondOperand = null;
firstOperator = null;
secondOperator = null;
result = null;
}
} else {
secondOperand = displayValue;
result = operate(Number(firstOperand), Number(secondOperand), firstOperator);
if (result === `don't / 0`) {
displayValue = `don't / 0`;
} else {
displayValue = accuratelyRound(result, 15).toString();
firstOperand = displayValue;
secondOperand = null;
firstOperator = null;
secondOperator = null;
result = null;
}
}
};
//Function for inputing decimals.
function inputDecimals (dot) {
if (displayValue === firstOperand || displayValue === secondOperand) {
displayValue = 0;
displayValue += dot;
} else if (!displayValue.includes(dot)) {
displayValue += dot;
}
};
//Function for sign
function inputSign (num) {
displayValue = (num * -1).toString();
};
//Function for percentage.
function inputPercent (num) {
displayValue = (num / 100).toString();
}
//Clear display function / reset all variables
function clearDisplay() {
displayValue = 0;
firstOperand = null;
secondOperand = null;
firstOperator = null;
secondOperator = null;
result = null;
};
//operate function to be called in other functions
function operate (a, b, op) {
if (op === "+") {
return a + b;
} else if (op === "-") {
return a - b;
} else if (op === "/") {
if (b === 0) {
return `don't / 0`
} else {
return a / b;
}
} else if (op === "*") {
return a * b;
}
};
//Round results accurately function to be called in other functions.
function accuratelyRound(num, places) {
return parseFloat(Math.round(num + `e` + places) + `e-` + places);
}