-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
122 lines (103 loc) · 3.75 KB
/
script.js
File metadata and controls
122 lines (103 loc) · 3.75 KB
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
const numberButtons = [...document.querySelectorAll('[data-number]')];
const operatorButtons = [...document.querySelectorAll('[data-operator]')];
const resultDisplay = document.querySelector('#result-display');
const firstDisplay = document.querySelector('#first-display');
const clearAllButton = document.querySelector('#all-clear');
const equalButton = document.querySelector('#equal');
const deleteButton = document.querySelector('#clear');
const percentButton = document.querySelector('#percent');
let firstOperand = '';
let secondOperand = '';
let currentOperator = null;
document.addEventListener('keydown', handleKeyboardInput);
numberButtons.forEach(button => button.addEventListener('click', () => appendNumber(button.textContent)));
operatorButtons.forEach(button => button.addEventListener('click', () => setOperator(button.textContent)));
clearAllButton.addEventListener('click', clear);
equalButton.addEventListener('click', compute);
deleteButton.addEventListener('click', deleteText);
percentButton.addEventListener('click', percent);
function percent() {
resultDisplay.innerText = resultDisplay.innerText / 100;
}
function clear() {
firstOperand = '';
secondOperand = '';
currentOperator = null;
firstDisplay.innerText = '';
resultDisplay.innerText = '';
}
function deleteText() {
resultDisplay.innerText = resultDisplay.innerText.toString().slice(0, -1);
}
function clearResultDisplay() {
resultDisplay.innerText = '';
}
function appendNumber(number) {
if(number === '.' && resultDisplay.innerText.includes('.')) {
return;
}
if (number === '0' && resultDisplay.innerText === '0') {
return;
}
resultDisplay.innerText += number;
}
function setOperator(operator) {
if (currentOperator !== null) compute();
firstOperand = resultDisplay.innerText;
currentOperator = operator;
firstDisplay.innerText = `${firstOperand} ${currentOperator}`;
clearResultDisplay();
}
function compute() {
let result;
if (currentOperator === null || firstOperand === '') return;
secondOperand = resultDisplay.textContent;
firstDisplay.innerText = `${firstOperand} ${currentOperator} ${secondOperand} =`;
result = Math.round(operate(currentOperator, firstOperand, secondOperand) * 100) / 100;
resultDisplay.innerText = result;
currentOperator = null;
}
function operate(currentOperator, firstOperand, secondOperand) {
firstOperand = Number(firstOperand);
secondOperand = Number(secondOperand);
switch (currentOperator) {
case '+':
return add(firstOperand, secondOperand);
case '-':
return subtract(firstOperand, secondOperand);
case 'x':
return multiply(firstOperand, secondOperand);
case '÷':
if (secondOperand === 0) return 'Error';
else return divide(firstOperand, secondOperand);
default:
return null;
}
}
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
function subtract(a, b) {
return a - b;
}
function divide(a, b) {
return a / b;
}
function handleKeyboardInput(event) {
if (event.key >= 0 && event.key <= 9) appendNumber(event.key);
if (event.key === '.') appendNumber(event.key);
if (event.key === '=' || event.key === 'Enter') compute();
if (event.key === 'Backspace') deleteText();
if (event.key === 'Escape') clear();
if (event.key === '+' || event.key === '-' || event.key === '*' || event.key === '/') {
setOperator(changeOperator(event.key));
}
}
function changeOperator(key) {
if (key === '/') return '÷';
if (key === '*') return 'x';
return key;
}