-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
163 lines (148 loc) · 5.01 KB
/
script.js
File metadata and controls
163 lines (148 loc) · 5.01 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//All button elements
const nbrBtns = document.querySelectorAll('.nbrBtn');
const plusBtn = document.querySelector('#plus');
const minusBtn = document.querySelector('#minus');
const timesBtn = document.querySelector('#times');
const divideBtn = document.querySelector('#divide');
const equalsBtn = document.querySelector('#equals');
//Top and bottom half of display elements. Top for operation and bottom for result
const calcDisplay = document.querySelector('.calc');
const resultDisplay = document.querySelector('.result');
let firstNbrString = '', secondNbrString = '', resultNbrString = '', operatorString = '';
let operatorStrings = ['plus', 'times', 'divide', 'minus'];
let awaitFirstNbr = true; //Makes sure a first number is inputted before operator is
let appendToPrevResult = false; //If operator is inputted before a new number it will use last result as first number
let canAppendDot = true; //Stops multiple dots on the same number
//Eventlisteners for all buttons (Clicking)
nbrBtns.forEach(button => button.addEventListener('click', (button) => handleNbrBtn(button.target.textContent)));
plusBtn.addEventListener('click', (plusBtn) => handleOperator(plusBtn.target.id));
minusBtn.addEventListener('click', (minusBtn) => handleOperator(minusBtn.target.id));
timesBtn.addEventListener('click', (timesBtn) => handleOperator(timesBtn.target.id));
divideBtn.addEventListener('click', (divideBtn) => handleOperator(divideBtn.target.id));
equalsBtn.addEventListener('click', handleEquals);
//Eventlistener for all buttons (numpad support)
document.addEventListener('keydown', (e) => {
switch (e.key) {
case '+':
handleOperator('plus');
break;
case '-':
handleOperator('minus');
break;
case '*':
handleOperator('times');
break;
case '/':
handleOperator('divide');
break;
case 'Enter':
handleEquals();
break;
case 'Delete': //Reset on delete
firstNbrString = '', secondNbrString = '', resultNbrString = '', operatorString = '';
awaitFirstNbr = true, appendToPrevResult = false, canAppendDot = true;
updateDisplay();
break;
default:
if (!isNaN(e.key) || e.key === ',') handleNbrBtn(document.querySelector(`[id='${e.key}']`).textContent);
}
});
/*
Input: Button textcontent
Updates numbers on display from input.
*/
function handleNbrBtn(button) {
if (awaitFirstNbr && !appendToPrevResult) {
if (button === '.' && canAppendDot) {
firstNbrString += button;
canAppendDot = false;
} else if (button !== '.') {
firstNbrString += button;
}
} else if (appendToPrevResult) {
firstNbrString = button;
appendToPrevResult = false;
} else {
if (button === '.' && canAppendDot) {
secondNbrString += button;
canAppendDot = false;
} else if (button !== '.') {
secondNbrString += button;
}
} updateDisplay();
}
/*
Input: Operator character.
Updates display with operator input.
*/
function handleOperator(operator) {
if ((awaitFirstNbr && firstNbrString !== '') || appendToPrevResult) {
awaitFirstNbr = false;
appendToPrevResult = false;
canAppendDot = true;
operatorString = operator;
updateDisplay();
}
};
/*
If second number has a value: evaluate operation.
*/
function handleEquals() {
if (secondNbrString != '') {
resultNbrString = operate(firstNbrString, secondNbrString, operatorString);
appendToPrevResult = true;
awaitFirstNbr = true;
updateDisplay();
firstNbrString = resultNbrString;
secondNbrString = '', operatorString = '';
}
};
/*
Updates top and bottom halfs of display with current operation and result
*/
function updateDisplay() {
calcDisplay.textContent = `${firstNbrString} ${((operatorString === '') ? '' : operatorChars[operatorString])} ${secondNbrString}`;
resultDisplay.textContent = resultNbrString;
}
//Object for converting operator strings to correct characters for display
const operatorChars = {
equals: '=',
plus: '+',
minus: '-',
times: 'x',
divide: '÷',
}
/*
Takes numbers and operator as string and calls for operation functions.
Returns result of operation
*/
function operate(a, b, operator) {
switch (operator) {
case 'plus':
return add(Number(a), Number(b));
break;
case 'times':
return multiply(Number(a), Number(b));
break;
case 'minus':
return subtract(Number(a), Number(b));
break;
case 'divide':
return division(Number(a), Number(b));
break;
default:
console.warn('Default operator case, something is wrong');
}
}
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
function subtract(a, b) {
return a - b;
}
function division(a, b) {
return a / b;
}