-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (31 loc) · 982 Bytes
/
index.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
console.log('Greetings! It\'s a Version: 1.0.0');
function reversePolishNotation(value) {
const operatorsValue = value.split(' ');
const calculatedValues = [];
operatorsValue.forEach(operatorValue => {
if (!isNaN(operatorValue)) {
calculatedValues.push(parseFloat(operatorValue));
} else {
const secondValue = calculatedValues.pop();
const firstValue = calculatedValues.pop();
switch (operatorValue) {
case '+':
calculatedValues.push(firstValue + secondValue);
break;
case '-':
calculatedValues.push(firstValue - secondValue);
break;
case '*':
calculatedValues.push(firstValue * secondValue);
break;
case '/':
calculatedValues.push(firstValue / secondValue);
break;
default:
throw new Error('Invalid operator');
}
}
});
return calculatedValues.pop();
}
module.exports = reversePolishNotation;