-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
177 lines (142 loc) · 4.43 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
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
const screen = document.querySelector('.screen');
let display = document.createElement('p');
display.setAttribute('class', 'display');
screen.appendChild(display);
display.textContent = '0'
let displayNumber;
const clearAll = document.querySelector('.clear-all');
const operators = document.querySelectorAll('.operator');
const digits = document.querySelectorAll('.digit');
const decimalPoint = document.querySelector('.decimal');
const equalsButton = document.querySelector('.equals-key');
const negator = document.querySelector('.negator');
const percentOperator = document.querySelector('.percentage');
const deleteKey = document.querySelector('.delete-key');
const allButtons = document.querySelector('.button');
let num1 = '';
let num2 = '';
let currentOperator = null;
let clearScreen = false;
clearAllScreen = () => {
display.textContent = '0';
clearScreen = true;
num1 = '';
num2 = '';
currentOperator = null;
}
resetScreen = () => {
display.textContent = '';
clearScreen = false;
}
add = (num1, num2) => {
let sum = num1 + num2;
return sum;
}
subtract = (num1, num2) => {
let difference = num1 - num2;
return difference;
}
multiply = (num1, num2) => {
let product = num1 * num2;
return product;
}
let divide = (num1, num2) => {
let quotient = num1 / num2;
return quotient;
}
operate = (num1, num2, operator) => {
// operator = document.querySelector('.operator')
switch (operator) {
case "+":
return add(num1, num2);
case "-":
return subtract(num1, num2);
case "*":
return multiply(num1, num2);
case "/":
return divide(num1, num2);
}
}
setOperator = (operator) => {
if (currentOperator !== null) performCalculation();
num1 = Number(display.textContent);
currentOperator = operator;
clearScreen = true;
}
function roundNumber(number) {
return Math.round(number * 1000) / 1000;
}
function performCalculation() {
if (currentOperator === null || clearScreen) return;
num2 = Number(display.textContent);
display.textContent = roundNumber(operate(num1, num2, currentOperator));
currentOperator = null;
}
addNegation = () => {
display.textContent = Number(-display.textContent);
}
calculatePercentage = (number) => {
number = display.textContent
let percentage = roundNumber(number / 100);
display.textContent = percentage;
return percentage;
}
addDecimal = () => {
if (display.textContent === '') display.textContent = '0';
if (display.textContent.includes('.')) return;
display.textContent += '.';
return parseFloat(display.textContent);
}
displayOnScreen = (e) => {
let number = e.target.innerText;
if (display.textContent === '0' || clearScreen) resetScreen();
display.textContent += number;
}
deleteValues = () => {
display.textContent = display.textContent.toString().slice(0, -1);
}
function addKeyboardSupport(e) {
// console.log(e.key)
digits.forEach(digit => {
if (digit.innerText === e.key) {
if (display.textContent === "0" || clearScreen) resetScreen();
display.textContent = display.textContent + e.key;
}
})
operators.forEach(operator => {
if (operator.textContent == e.key) setOperator(operator.textContent);
})
switch (e.key) {
case "Backspace":
return deleteValues()
break;
case "%":
return calculatePercentage();
case "_":
return addNegation();
case "Escape":
return clearAllScreen();
case "Enter":
return performCalculation();
case ".":
return addDecimal();
default:
break;
}
}
for (let i = 0; i < digits.length; i++) {
digits[i].addEventListener("click", displayOnScreen);
}
operators.forEach(operator => operator.addEventListener('click', () => setOperator(operator.textContent)))
equalsButton.addEventListener('click', performCalculation);
clearAll.addEventListener('click', clearAllScreen);
negator.addEventListener('click', addNegation);
percentOperator.addEventListener('click', calculatePercentage);
decimalPoint.addEventListener('click', addDecimal);
deleteKey.addEventListener('click', deleteValues);
window.addEventListener('keydown', addKeyboardSupport);
// Adding Gradient Background
randomNumber = (min, max) => {
let random = Math.floor(Math.random() * (max - min + 1)) + min;
return random;
}