Advertisement
dimoBs

02. Array Modifier

Feb 22nd, 2021
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let array = input.shift().split(' ').map(Number);
  4.  
  5.     input.forEach(element => {
  6.         element = element.split(' ');
  7.  
  8.         let [a, b, c] = element;
  9.        
  10.         if (a === 'decrease') {
  11.             array = array.map(function (x) {
  12.                 return x - 1
  13.             });
  14.  
  15.         } else if (a === 'swap') {
  16.             let valueB = array[b];
  17.             let valueC = array[c];
  18.             array.splice(b, 1, valueC);
  19.             array.splice(c, 1, valueB);
  20.  
  21.         } else if (a === 'multiply') {
  22.             let tempoA = array[b];
  23.             let tempoB = array[c];
  24.             let result = tempoA * tempoB;
  25.             array.splice(b, 1, result);
  26.  
  27.         } else if (a === 'end') {
  28.             console.log(array.join(', '));
  29.         }
  30.     });
  31.  
  32. }
  33.  
  34. solve(['1 2 3 4', 'swap 0 1', 'swap 1 2', 'swap 2 3', 'multiply 1 2', 'decrease', 'end']);
  35. solve(['23 -2 321 87 42 90 -123', 'swap 1 3', 'swap 3 6', 'swap 1 0', 'multiply 1 2', 'multiply 2 1', 'decrease', 'end']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement