Advertisement
viligen

jansNotation

May 28th, 2022
1,083
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function myFunc(inputArr) {
  2.     const resultingArr = [];
  3.     const operators = {
  4.         "*": (n1, n2) => n1 * n2,
  5.         "/": (n1, n2) => n1 / n2,
  6.         "+": (n1, n2) => n1 + n2,
  7.         "-": (n1, n2) => n1 - n2,
  8.     };
  9.  
  10.     while (inputArr.length > 0) {
  11.         let sign = inputArr.shift();
  12.         if (!operators.hasOwnProperty(sign)) {
  13.             resultingArr.push(sign);
  14.             continue;
  15.         }
  16.         let num2 = resultingArr.pop();
  17.         let num1 = resultingArr.pop();
  18.         if (num1 == undefined || num2 == undefined) {
  19.             console.log("Error: not enough operands!");
  20.             return;
  21.         }
  22.         resultingArr.push(operators[sign](num1, num2));
  23.     }
  24.     if (resultingArr.length > 1) {
  25.         console.log("Error: too many operands!");
  26.         return;
  27.     }
  28.     console.log(resultingArr[0]);
  29. }
  30.  
  31. myFunc([15, "/"]);
  32.  
Advertisement
RAW Paste Data Copied
Advertisement