Advertisement
Spocoman

04. Jan's Notation

Dec 17th, 2022
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function janNotation(input) {
  2.     for (let i = 0; i < input.length; i++) {
  3.         let [firstOperand, secondOperand, operator] = input.slice(i, i + 3);
  4.         if (typeof (firstOperand + secondOperand) === 'number') {
  5.             if (typeof (operator) === 'string') {
  6.                 operator === '+' ? firstOperand += secondOperand :
  7.                     operator === '-' ? firstOperand -= secondOperand :
  8.                         operator === '*' ? firstOperand *= secondOperand :
  9.                             firstOperand /= secondOperand;
  10.                 input.splice(i, 3, firstOperand);
  11.                 i = -1;
  12.             }
  13.         } else {
  14.             return console.log('Error: not enough operands!');
  15.         }
  16.     }
  17.     console.log(input.length === 1 ? input[0] : 'Error: too many operands!');
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement