Feb 17th, 2022
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function solve(array) {
2.     let numbersArray = [];
3.     for (let i = 0; i < array.length; i++){
4.         if (Number(array[i])){
5.             numbersArray.push(Number(array[i]));
6.         }
7.         else {
8.             DoMath(numbersArray, array[i]);
9.         }
10.     }
11.     if (numbersArray.length >= 2){
12.         console.log('Error: too many operands!')
13.     }
14.     else if (numbersArray.length !== 0) {
15.         console.log(numbersArray.toString());
16.     }
17.     function DoMath(numbers, operator) {
18.         if(numbers.length < 2) {
19.             console.log('Error: not enough operands!');
20.         }
21.         if (operator === '+'){
22.             numbers[numbers.length - 2] = numbers[numbers.length - 2] + numbers[numbers.length - 1];
23.             numbers.splice(- 1, 1);
24.         }
25.         else if (operator === '-'){
26.             numbers[numbers.length - 2] = numbers[numbers.length - 2] - numbers[numbers.length - 1];
27.             numbers.splice(- 1, 1);
28.         }
29.         else if (operator === '*'){
30.             numbers[numbers.length - 2] = numbers[numbers.length - 2] * numbers[numbers.length - 1];
31.             numbers.splice(- 1, 1);
32.         }
33.         else if (operator === '/'){
34.             numbers[numbers.length - 2] = numbers[numbers.length - 2] / numbers[numbers.length - 1];
35.             numbers.splice(- 1, 1);
36.         }
37.     }
38. }
39. solve([3,
40.     4,
41.     '+']
42.    );