Guest User

Untitled

a guest
Jun 21st, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arrayManipulator(numArray, commandArray) {
  2.     let command = commandArray.shift().split(' ');
  3.    
  4.     while (command[0] !== 'print') {
  5.         let currentCommand = command[0];
  6.  
  7.         switch (currentCommand) {
  8.             case 'add': {
  9.                 let index = command[1];
  10.                 let element = Number(command[2]);
  11.                 add(index, element, numArray);
  12.                 break;
  13.             }
  14.             case 'addMany': {
  15.                 command.shift();
  16.                 let elements = command.map(Number);
  17.                 let index = elements.shift();
  18.                 addMany(index, elements, numArray);
  19.                 break;
  20.             }
  21.             case 'contains': {
  22.                 let element = Number(command[1]);
  23.                 contains(element, numArray);
  24.                 break;
  25.             }
  26.             case 'remove': {
  27.                 let index = command[1];
  28.                 remove(index, numArray);
  29.                 break;
  30.             }
  31.             case 'shift': {
  32.                 let rotations = command[1];
  33.                 numArray = shift(rotations, numArray);
  34.                 break;
  35.             }
  36.             case 'sumPairs': {
  37.                 numArray = sumPairs(numArray);
  38.                 break;
  39.             }
  40.         }
  41.  
  42.         command = commandArray.shift().split(' ');
  43.     }
  44.  
  45.     console.log(`[ ${numArray.join(', ')} ]`);
  46.  
  47.     function add(index, element, array) {
  48.         return array.splice(index, 0, element);
  49.     }
  50.  
  51.     function addMany(index, elements, array) {
  52.         for (let element of elements) {
  53.             array.splice(index, 0, element);
  54.             index++;
  55.         }
  56.     }
  57.  
  58.     function contains(element, array) {
  59.         if (array.includes(element)) {
  60.             console.log(array.indexOf(element));
  61.         } else {
  62.             console.log(-1);
  63.         }
  64.     }
  65.  
  66.     function remove(index, array) {
  67.         return array.splice(index, 1);
  68.     }
  69.  
  70.     function shift(rotations, array) {
  71.         let realRotations = rotations % array.length;
  72.         let result = [];
  73.  
  74.         for (let i = 0; i < array.length; i++) {
  75.             let currentElement = array[i];
  76.  
  77.             if (i >= realRotations) {
  78.                 result.push(currentElement);
  79.             }
  80.         }
  81.  
  82.         for (let j = 0; j < array.length; j++) {
  83.             let currentElement = array[j];
  84.  
  85.             if (j < realRotations) {
  86.                 result.push(currentElement);
  87.             } else {
  88.                 break;
  89.             }
  90.         }
  91.  
  92.         return result;
  93.     }
  94.  
  95.     function sumPairs(array) {
  96.         let result = [];
  97.  
  98.         for (let i = 0; i < array.length; i += 2) {
  99.             let currentElement = array[i];
  100.             let nextElement = array[i + 1];
  101.             let currentSum = currentElement + nextElement;
  102.             if (nextElement !== undefined) {
  103.                 result.push(currentSum);
  104.             } else {
  105.                 result.push(currentElement);
  106.             }
  107.         }
  108.  
  109.         return result;
  110.     }
  111. }
Add Comment
Please, Sign In to add comment