-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
193 lines (170 loc) · 4.3 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
185
186
187
188
189
190
191
192
193
const buttons = document.querySelectorAll('button');
const result = document.querySelector('.calc-result');
let functionStr = document.querySelector('.calc-function');
let isOperator = false;
const operators = {
add: "+",
sub: "-",
mul: "×",
div: "÷",
ext: "^",
perc: "%"
}
function checkOperator(str){
let isOperator = false;
for(const operator in operators){
if(str.includes(operators[operator])){
isOperator = true;
}
}
return isOperator;
}
function calculate(str) {
let arr = str.split(" ");
let a = getNumber(arr[0]);
let b = getNumber(arr[2]);
let operator = arr[1];
let equation;
switch (operator) {
case "+":
equation = sum(a, b);
break;
case "-":
equation = substract(a, b);
break;
case "×":
equation = mulitply(a, b);
break;
case "÷":
equation = divide(a, b);
break;
case "^":
equation = extrapolate(a, b);
break;
case "%":
equation = percentile(a);
break;
}
return roundNumber(equation);
}
function sum(a, b){
return a + b;
}
function substract(a, b){
return a - b;
}
function mulitply(a, b){
return a * b;
}
function divide(a, b){
if(b === 0){
return "LMFAO";
}
else{
return a / b;
}
}
function extrapolate(a, b){
return a ** b;
}
function percentile(a){
return a / 100;
}
function roundNumber(number){
if(number % 1 != 0){
try{
number = number.toFixed(2);
} catch(error){ };
}
return number;
}
function getNumber(number){
const parsedNumber = parseFloat(number);
if(!isNaN(parsedNumber)){
return parsedNumber;
}
}
function populateDisplay(){
buttons.forEach((button) => {
button.addEventListener('click', () => checkDisplay(button));
});
}
function clearValues(){
result.textContent = 0;
functionStr.textContent = "";
}
function deleteValues(){
let arr = result.textContent.split("");
arr.pop();
result.textContent = arr.join("");
}
function addNumber(number){
if(result.textContent == 0 && !result.textContent.includes(".")){
result.textContent = number
}
else if(isOperator && checkOperator(functionStr.textContent)){
result.textContent = number;
isOperator = false;
}
else if(!functionStr.textContent.includes("=")){
result.textContent += number;
}
}
function addOperator(operator){
if(!checkOperator(functionStr.textContent)){
functionStr.textContent += result.textContent + " " + operator + " ";
isOperator = true;
}
else if(checkOperator(functionStr.textContent) && isOperator){
functionStr.textContent = result.textContent + " " + operator + " ";
}
else if(!isOperator){
result.textContent = calculate(functionStr.textContent + result.textContent);
functionStr.textContent = result.textContent + " " + operator + " ";
isOperator = true;
}
}
function addDecimal(){
if(!result.textContent.includes(".")){
result.textContent += ".";
}
}
function setUpEquation(){
functionStr.textContent = functionStr.textContent + result.textContent + " " + "=";
result.textContent = calculate(functionStr.textContent + result.textContent);
}
function directEquation(operator){
functionStr.textContent = result.textContent + " " + operator + " " + "=";
result.textContent = calculate(functionStr.textContent);
}
function stringInUse(str){
return functionStr.textContent.includes(str);
}
function stringEmpty(){
return functionStr.textContent.length === 0;
}
function checkDisplay(button){
let userChoice = button.textContent;
if(!isNaN(userChoice)){
addNumber(userChoice);
}
else if (userChoice === "%"){
directEquation("%");
}
else if(userChoice === "=" && !stringEmpty() && !stringInUse("=")){
setUpEquation();
}
else if(userChoice === "C" && !stringInUse("=")){
deleteValues();
}
else if(userChoice === "CE"){
clearValues();
}
else if(userChoice === "."){
addDecimal();
}
else if(checkOperator(userChoice)){
addOperator(userChoice);
}
}
populateDisplay();