forked from woowacourse/javascript-calculator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.js
More file actions
30 lines (30 loc) · 850 Bytes
/
utils.js
File metadata and controls
30 lines (30 loc) · 850 Bytes
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
const isOperator = (value) => {
const allowedKey = ['+', '-', '/', 'X', '='];
return allowedKey.indexOf(value) !== -1;
};
const isClear = (value) => {
const allowedKey = 'AC';
return allowedKey === value;
};
const operate = (prev, oper, value) => {
const num_prev = +prev;
const num_value = +value;
switch (oper) {
case '+':
return String(num_prev + num_value);
case '-':
return String(num_prev - num_value);
case 'X':
return String(num_prev * num_value);
case '/':
return String(Math.floor(num_prev / num_value));
default:
return prev;
}
};
const calc = (prev, oper, value) => {
if (prev === '' || oper === '')
return value;
return operate(prev, oper, value);
};
export { isOperator, isClear, calc };