-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
206 lines (174 loc) · 5.72 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
194
195
196
197
198
199
200
201
202
203
204
205
206
var body = document.querySelector("body");
var numPad = document.querySelector(".num-pad");
var calculatorDisplay = document.querySelector("#result");
var calculation = document.querySelector("#calculation");
var buttonsAll = document.querySelectorAll('.num-button');
var clearButton = document.querySelector('#clear');
var backSpaceButton = document.querySelector('#back-space');
var equal = document.querySelector('#equal');
var changeSign = document.querySelector('#change-sign');
var currentOperation = "+"
var operationSign = document.querySelector("#operation-sign");
const keyBoardNumbers = {
// Here are the keyboard numbers and the dot(".")
// to be used by the keyboard
96: 0,
97: 1,
98: 2,
99: 3,
100: 4,
101: 5,
102: 6,
103: 7,
104: 8,
105: 9,
48: 0,
49: 1,
50: 2,
51: 3,
52: 4,
53: 5,
54: 6,
55: 7,
56: 8,
57: 9,
110: ".",
190: "."
};
const keyBoardValues = {
8: backSpace
}
const operators = {
//The operators used by the keyboard
107: "+",
109: "-",
111: "÷",
106: "*"
};
var currentOperation = "+";
var currentNumber = 0;
var numbers = [...buttonsAll].filter(element => !isNaN(parseInt(element.textContent.trim())));
function checkIfZero() {
if (calculatorDisplay.textContent.trim() == "0") calculatorDisplay.textContent = "";
}
function backSpace() {
let length = calculatorDisplay.textContent.length;
calculatorDisplay.textContent = calculatorDisplay.textContent.slice(0, length - 1);
if (calculatorDisplay.textContent.length < 1) calculatorDisplay.textContent = 0;
}
function equalUsage() {
//this is the main function that is used
// to achieve the result of the operation
fontDisplayLenght(calculatorDisplay);
calculatorDisplay.textContent = operate(currentOperation, currentNumber, calculatorDisplay.textContent);
currentNumber = calculatorDisplay.textContent;
calculation.textContent = currentNumber;
}
function fontDisplayLenght(part) {
//Here if the numbers get too large the fontsize will decrease
// so that there won't be any overflow from the calculator
// to the main background
if (part.textContent.length > 15) part.style.fontSize = "16px";
}
function sum(first, ...args) {
//Function used by the "+" sign
return args.reduce((total, current) => {
return total + current;
}, first);
}
function substract(first, ...args) {
//Function used by the "+" sign
return args.reduce((total, current) => {
return total - current;
}, first);
}
function multiply(first, ...args) {
//Function used by the "+" sign
return args.reduce((total, current) => {
return total * current;
}, first);
}
function division(first, ...args) {
//Function used by the "+" sign
return args.reduce((total, current) => {
return total / current;
}, first);
}
function operate(operator, first, ...args) {
//Here will the operations execute
//The function that matches the operator
// will be executed
first = parseFloat(first);
args = [...args].map(element => parseFloat(element));
let rv = "";
switch (operator) {
case "+":
rv = sum(first, ...args);
break;
case "-":
rv = substract(first, ...args);
break;
case "*":
rv = multiply(first, ...args);
break;
case "÷":
rv = division(first, ...args);
break;
default:
break;
}
return rv;
}
function operationChange(operation) {
// This is to change the operator displayed on the calculator screen
operationSign.textContent = operation;
currentOperation = operation;
currentNumber = calculatorDisplay.textContent;
calculatorDisplay.textContent = 0;
calculation.textContent = currentNumber;
}
[...buttonsAll].filter(operator => Object.values(operators).includes(operator.textContent.trim()))
.forEach(operator => {
//Here the functions for each operator are beign assigned
operator.addEventListener("click", () => operationChange(operator.textContent.trim()));
});
clearButton.addEventListener('click', () => {
//this is for the clear button(C)
calculation.textContent = "";
calculatorDisplay.textContent = 0;
currentNumber = 0;
})
backSpaceButton.addEventListener('click', backSpace);// adding backspace functionality to the backspace button
body.addEventListener('keydown', (e) => {
//Using keyboard to enter numbers and operations.
if (e.keyCode in keyBoardNumbers) {
//Entering numbers and dot(".")
checkIfZero()
if (calculatorDisplay.textContent.includes(".") && (e.keyCode == 190 || e.keyCode == 110)) return;
calculatorDisplay.textContent += keyBoardNumbers[e.keyCode];
}
else if (e.keyCode in keyBoardValues) {
//For backspace
checkIfZero()
keyBoardValues[e.keyCode]();
}
else if (e.keyCode in operators) {
//for operators
operationChange(operators[e.keyCode]);
}
else if (e.keyCode == 13) equalUsage(); //For getting the result using enter
});
numbers.forEach(number => {
number.addEventListener('click', () => {
//Giving number buttons on the calculator functionality
checkIfZero();
fontDisplayLenght(calculatorDisplay);
if (calculatorDisplay.textContent.includes(".")) return;
calculatorDisplay.textContent += number.textContent.trim();
})
})
equal.addEventListener('click', () => equalUsage());//Giving equal button on the calculator functionality
changeSign.addEventListener('click', () => {
//changin the operator on the calculator display
calculatorDisplay.textContent = -calculatorDisplay.textContent;
})