-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
155 lines (121 loc) · 3.54 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
let firstOperand = '';
let secondOperand = '';
let operatorSign = null;
let shouldScreenReset = false;
let didEqual = false;
const clearButton = document.getElementById('clearButton');
const deleteButton = document.getElementById('deleteButton');
const lastCalc = document.getElementById('lastCalc');
const currentCalc = document.getElementById('currentCalc');
const buttonArray = document.querySelectorAll('[data-number]');
const operatorArray = document.querySelectorAll('[data-operator]');
const equalButton = document.getElementById('equalButton');
const dotButton = document.getElementById('dotButton');
clearButton.addEventListener('click', clear);
deleteButton.addEventListener('click', deleteItem);
equalButton.addEventListener('click', doEquals);
dotButton.addEventListener('click', addDot)
buttonArray.forEach(button => {
button.addEventListener('click', () => {
if (didEqual === true) {
currentCalc.textContent = '';
}
addNumber(button.textContent);
didEqual = false;
});
});
operatorArray.forEach(operator => {
operator.addEventListener('click', () => setOperation(operator.textContent));
});
function add(input1, input2) {
return input1 + input2;
}
function divide(input1, input2) {
return input1/input2;
}
function multiply(input1, input2) {
return input1 * input2;
}
function subtract(input1, input2) {
return input1 - input2;
}
function clear() {
currentCalc.textContent = '0';
lastCalc.textContent = '';
operatorSign = null;
firstOperand = '';
secondOperand = '';
shouldScreenReset = false;
}
function doOperation(operator, num1, num2) {
num1 = Number(num1);
num2 = Number(num2);
switch (operator) {
case '+':
return add(num1, num2);
case '-':
return subtract(num1, num2);
case '÷':
return divide(num1, num2);
case 'x':
return multiply(num1, num2);
default:
return null;
}
};
function resetScreen() {
if (shouldScreenReset == true) {
currentCalc.textContent = '';
shouldScreenReset = false;
}
}
function addNumber(number) {
if (shouldScreenReset) {
resetScreen();
}
else if (currentCalc.textContent == '0') {
currentCalc.textContent = '';
}
currentCalc.textContent += number;
}
function deleteItem() {
currentCalc.textContent = currentCalc.textContent.slice(0, -1) || '0';
}
function setOperation(operator) {
if (operatorSign !== null) {
doEquals();
}
firstOperand = currentCalc.textContent;
operatorSign = operator;
lastCalc.textContent = `${firstOperand} ${operatorSign}`;
shouldScreenReset = true;
}
function doEquals() {
didEqual = true;
if (operatorSign === null || shouldScreenReset) {
return;
}
secondOperand = currentCalc.textContent;
if (operatorSign === '÷' && currentCalc.textContent === '0') {
alert("You can't divide by 0!");
return;
}
currentCalc.textContent = roundNumber(doOperation(operatorSign, firstOperand, secondOperand));
lastCalc.textContent = `${firstOperand} ${operatorSign} ${secondOperand} =`;
operatorSign = null;
}
function addDot() {
if (shouldScreenReset) {
resetScreen();
}
if (currentCalc.textContent === '') {
currentCalc.textContent = '0';
}
if (currentCalc.textContent.includes('.')) {
return;
}
currentCalc.textContent += '.';
}
function roundNumber(num) {
return Math.round(num * 100000) / 100000;
}