RRusev77

WtfJS

Jul 1st, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function manipulateArray(array, commands) {
  2.     const add = (index, element) => {
  3.         array.splice(index, 0, element);
  4.     }
  5.  
  6.     const addMany = (inputOne, inputTwo) => {
  7.         inputOne = Number(inputOne);
  8.        
  9.         inputTwo = inputTwo.map(Number);
  10.  
  11.         array.splice(inputOne, 0, ...inputTwo);
  12.     }
  13.  
  14.     const contains = number => {
  15.         if (array.includes(number)) {
  16.             let index = array.indexOf(number);
  17.             console.log(index);
  18.         } else {
  19.             console.log(-1);
  20.         }
  21.     }
  22.  
  23.     const remove = index => {
  24.         array.splice(index, 1);
  25.     }
  26.  
  27.     const shift = position => {
  28.         for(let i = 0; i < position; i++) {
  29.             array.push(array.shift());
  30.         }
  31.     }
  32.  
  33.     const sumPairs = () => {
  34.         array = array.map((e, i, array) => i < array.length - 1 ? (array[i] += array[i + 1]) : array[i] = array[i]).filter((e, i) => i % 2 === 0);
  35.     }
  36.  
  37.     for (let currentCommand of commands) {
  38.         let wordsInCommand = currentCommand.split(' ');
  39.         let firstWord = wordsInCommand.shift();
  40.  
  41.         if (firstWord == 'add') {
  42.             let index = Number(wordsInCommand[0]);
  43.             let element = Number(wordsInCommand[1]);
  44.             add(index, element);
  45.         } else if (firstWord == 'addMany') {
  46.             let index = Number(wordsInCommand.shift());
  47.             addMany(index, wordsInCommand);
  48.         } else if (firstWord == 'contains') {
  49.             let element = Number(wordsInCommand[0]);
  50.             contains(element);
  51.         } else if (firstWord == 'remove') {
  52.             let index = Number(wordsInCommand[0]);
  53.             remove(index);
  54.         } else if (firstWord == 'shift') {
  55.             let position = Number(wordsInCommand[0]);
  56.             shift(position);
  57.         } else if (firstWord == 'sumPairs') {
  58.             sumPairs();
  59.         } else if (firstWord == 'print'){
  60.             array = array.join(', ');
  61.             console.log(`[ ${array} ]`);
  62.             break;
  63.         }
  64.     }
  65. }
Add Comment
Please, Sign In to add comment