Advertisement
Lulunga

Array Manipulator

Jun 28th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arrayManipulations(arr, input) {
  2.     let nums = [];
  3.     let numb;
  4.     for (let operations of input) {
  5.  
  6.         let splitOperations = operations.split(' ');
  7.         let command = splitOperations[0];
  8.         if (command === "addMany") {
  9.             for (let i = 0; i < splitOperations.length; i++) {
  10.                 nums.push(splitOperations[i]);
  11.             }
  12.             nums.shift();
  13.             numb = nums.slice(1)
  14.  
  15.  
  16.             for (let j = numb.length - 1; j >= 0; j--) {
  17.                 arr.splice(splitOperations[1], 0, Number(numb[j]));
  18.             }
  19.  
  20.         }
  21.  
  22.         if (command === "add") {
  23.             arr.splice(splitOperations[1], 0, +splitOperations[2]);
  24.         }
  25.         if (command === "contains") {
  26.             for (let i = 0; i < arr.length; i++) {
  27.                 if (+arr[i] === +splitOperations[1]) {
  28.                     console.log(i)
  29.                     break;
  30.                 } else {
  31.                     console.log(-1);
  32.                     break;
  33.                 }
  34.             }
  35.         }
  36.         if (command === "remove") {
  37.             arr.splice(+splitOperations[1], 1);
  38.         }
  39.         if (command === "shift") {
  40.             for (let i = 0; i < +splitOperations[1]; i++) {
  41.                 let pushedElement = arr.shift();
  42.                 arr.push(pushedElement);
  43.             }
  44.         }
  45.         if (command === "sumPairs") {
  46.             arr = arr.map((e, i, arr) => i < arr.length - 1 ? (arr[i] += arr[i + 1]) : arr[i] = arr[i]).filter((e, i) => i % 2 === 0);
  47.         }
  48.         if (command === "print") {
  49.             break;
  50.  
  51.         }
  52.  
  53.  
  54.  
  55.  
  56.     }
  57.     console.log(arr)
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement