Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function janNotation(input) {
- for (let i = 0; i < input.length; i++) {
- let [firstOperand, secondOperand, operator] = input.slice(i, i + 3);
- if (typeof (firstOperand + secondOperand) === 'number') {
- if (typeof (operator) === 'string') {
- operator === '+' ? firstOperand += secondOperand :
- operator === '-' ? firstOperand -= secondOperand :
- operator === '*' ? firstOperand *= secondOperand :
- firstOperand /= secondOperand;
- input.splice(i, 3, firstOperand);
- i = -1;
- }
- } else {
- return console.log('Error: not enough operands!');
- }
- }
- console.log(input.length === 1 ? input[0] : 'Error: too many operands!');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement