-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
159 lines (137 loc) · 3.68 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
const display = document.querySelector('#display');
let currentValue = "";
let currentOperator;
let previousValue = "";
let eraseCurrentValue = false;
const deleteBtn = document.querySelector('#delete');
const equals = document.querySelector('#equals');
const dot = document.querySelector('#dot');
const numbers = document.querySelectorAll('.number');
const operators = document.querySelectorAll('.operator');
function deleteDigit(){
if (currentValue.length == 0 || currentValue.length == 1){
clearDisplay();
}else if (currentValue != "0") {
currentValue = currentValue.slice(0, -1);
populateDisplay(currentValue);
};
};
function addDot(){
currentValue += '.';
populateDisplay(currentValue);
dot.disabled = true;
};
dot.addEventListener("click", () => {
addDot();
});
deleteBtn.addEventListener("click", () => {
deleteDigit();
});
equals.addEventListener('click', () => {
if (previousValue != "" && currentValue != ""){
operate();
}
});
function populateDisplay (currentValue){
if (display.textContent == "0" || display.textContent == 0)
display.textContent = " ";
display.textContent = currentValue;
}
function addNumber(n){
if (eraseCurrentValue == true) {
eraseCurrentValue = false;
currentValue = "";
}
let digit = (n);
currentValue += digit;
populateDisplay(currentValue);
}
numbers.forEach(number => {
number.addEventListener("click", n => {
addNumber(n.target.textContent);
});
});
function addOperator(o){
if (previousValue != "" && currentValue != ""){
operate();
}
currentOperator = o;
eraseCurrentValue = true;
if (previousValue == "") {
previousValue = currentValue;
};
dot.disabled = false;
}
operators.forEach(operator => {
operator.addEventListener("click", o => {
addOperator(o.target.textContent);
});
});
function add(a, b){
return Number(a) + Number(b);
};
function subtract(a, b){
return Number(a) - Number(b);
};
function divide(a, b){
if (b == "0") {
clearDisplay();
return display.textContent = 'Error! Cannot divide by 0';
}
return Number(a) / Number(b);
};
function multiply(a, b){
return Number(a) * Number(b);
};
function mod(a, b){
return Number(a) % Number(b);
};
function clearDisplay(){
display.textContent = " ";
currentValue = "";
previousValue = "";
dot.disabled = false;
populateDisplay("0");
}
function operate(){
let total;
switch (currentOperator) {
case '+':
total = add(previousValue, currentValue);
break
case '-':
total = subtract(previousValue, currentValue);
break
case '/':
total = divide(previousValue, currentValue);
break
case '*':
total = multiply(previousValue, currentValue);
break
case '%':
total = mod(previousValue, currentValue);
break
};
total = Math.round((total + Number.EPSILON) * 100) / 100;
if (total > 1000000) {
total.toExponential(10);
};
dot.disabled = false;
currentValue = "";
previousValue = total;
populateDisplay(total);
};
window.addEventListener('keydown', e =>{
if (e.key == 'Backspace'){
e.preventDefault();
deleteDigit();
} else if((e.key > -1 && e.key < 10) || (e.key > 95 && e.key < 106)){
addNumber(e.key);
} else if(e.key == 'Enter'){
operate();
} else if(e.key == '+' || e.key == '-' || e.key == '/' || e.key == '*' || e.key == '%'){
addOperator(e.key);
} else if(e.key == '.'){
addDot();
}
});