# Untitled

Oct 1st, 2022
1,006
0
120 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function calculate(arr) {
2.
3.     function calc(f) {
4.         return f;
5.     }
6.
8.     const subtraction = '-';
9.     const multiplication = '*';
10.     const division = '/';
11.
12.     let actionAdd = (a, b) => a + b;
13.     let actionSubtraction = (a, b) => a - b;
14.     let actionMultiplication = (a, b) => a * b;
15.     let actionDivision = (a, b) => a / b;
16.
17.     let num = [];
18.     let actions = [];
19.
20.     for (const iterator of arr) {
21.         if (typeof (iterator) == 'number') {
22.             num.push(iterator);
23.         } else {
24.             actions.push(iterator);
25.         }
26.     }
27.
28.     function recursion(operands, arr) {
29.         if (arr.length === 0) {
30.             return operands[0];
31.         }
32.
33.         let operation = arr.shift();
34.
35.         if (typeof (operation) == 'number') {
36.             operands.push(operation);
37.         } else {
38.             let operand2 = operands.pop();
39.             let operand1 = operands.pop();
40.
41.             if (operation == add) {
43.             } else if (operation == subtraction) {
44.                 operands.push(calc(actionSubtraction(operand1, operand2)));
45.             } else if (operation == multiplication) {
46.                 operands.push(calc(actionMultiplication(operand1, operand2)));
47.             } else if (operation == division) {
48.                 operands.push(calc(actionDivision(operand1, operand2)));
49.             }
50.         }
51.
52.         return recursion(operands, arr);
53.
54.     }
55.
56.     let operands = [];
57.
58.     if (num.length == actions.length + 1) {
59.         console.log(recursion(operands, arr));
60.     } else if (num.length > actions.length + 1) {
61.         console.log("Error: too many operands!");
62.     } else {
63.         console.log("Error: not enough operands!");
64.     }
65. }