-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
145 lines (127 loc) · 3.78 KB
/
Copy pathscript.js
File metadata and controls
145 lines (127 loc) · 3.78 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
let firstNumb = 0;
let secondNumb = 0;
let operator = null;
let shouldResetScreen = false;
// maximum digits allowed
const DIGITMAX = 17;
const screen = document.querySelector('#screen');
const numberBtn = document.querySelectorAll('[data-number]');
const operatorBtn = document.querySelectorAll('[data-operator]');
const equalBtn = document.querySelector('#equalBtn');
const allClearBtn = document.querySelector('#allClearBtn');
const percentBtn = document.querySelector('#percentBtn');
const deleteBtn = document.querySelector('#deleteBtn');
const decimalBtn = document.querySelector('#decimalBtn');
numberBtn.forEach(
(button) => button.addEventListener('click', () => appendNumber(button.textContent))
);
operatorBtn.forEach(
(button) => button.addEventListener('click', () => recordOperation(button.textContent))
);
equalBtn.onclick = () => calculate();
allClearBtn.onclick = () => clearScreen();
percentBtn.onclick = () => setPercentage(screen.textContent);
deleteBtn.onclick = () => deleteNumber();
decimalBtn.onclick = () => appendDecimal();
// basic calculation logic
function add(a, b) {
return a + b;
};
function subtract(a, b) {
return a - b;
};
function multiply(a, b) {
return a * b;
};
function divide(a, b) {
return a / b;
};
// function declaration
function roundResult(number) {
return Math.round(number * 1000) / 1000;
};
function setPercentage(number) {
screen.textContent = Math.round((number / 100) * 1000) / 1000;
};
function deleteNumber() {
screen.textContent = screen.textContent.toString().slice(0, -1);
};
function operate(a, operator, b) {
a = Number(a);
b= Number(b);
if (operator == "+") return add(a, b);
if (operator == "-") return subtract(a, b);
if (operator == "×") return multiply(a, b);
if (operator == "÷") return divide(a, b);
};
function appendNumber(string) {
if (screen.textContent.length >= DIGITMAX) return;
if (screen.textContent === '0'|| shouldResetScreen) {
resetScreen();
};
screen.textContent += string;
};
function resetScreen() {
screen.textContent = '';
shouldResetScreen = false;
};
function recordOperation(sign) {
if (operator !== null) calculate();
firstNumb = screen.textContent;
operator = sign;
shouldResetScreen = true;
};
function calculate() {
if (operator === null || shouldResetScreen) return;
if (screen.textContent === '0' && operator === '÷') {
alert("You can't divide by 0!");
return;
}
secondNumb = screen.textContent;
screen.textContent = roundResult(operate(firstNumb, operator, secondNumb));
operator = null;
};
function clearScreen() {
firstNumb = '';
operator = null;
secondNumb = '';
screen.textContent = 0;
};
function appendDecimal() {
if (screen.textContent === '') {
screen.textContent = '0';
}
if (screen.textContent.includes('.')) return
screen.textContent += '.';
};
window.addEventListener('keydown', handleKeyboardEvents);
function handleKeyboardEvents(e) {
if (e.key >= 0 && e.key <= 9) appendNumber(e.key);
if (e.key === '.') appendDecimal();
if (e.key === '=' || e.key === 'Enter') calculate();
if (e.key === 'Backspace') deleteNumber();
if (e.key === 'Escape') clearScreen();
if (e.key === '+' || e.key === '-' || e.key === '/' || e.key === '*') {
recordOperation(convertOperator(e.key));
};
if (e.key === 'Shift' && e.key === '+') {
recordOperation('+');
};
if (e.key === 'Shift' && e.key === '*') {
recordOperation('×');
};
};
function convertOperator(sign) {
switch (sign) {
case '/':
return '÷';
case '*':
return '×';
case '-':
return '-';
case '+':
return '+';
default:
return;
}
};