Advertisement
poli1993_

Array Manipulator

Oct 19th, 2019
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr,commands){
  2.    
  3.     for(let i = 0; i < commands.length; i++){
  4.         let splited = commands[i].split(' ');
  5.         let command = splited[0];
  6.             if(command === 'add'){
  7.                add();
  8.             }
  9.             else if(command === 'addMany'){
  10.                addMany();
  11.             }
  12.             else if(command === 'contains'){
  13.                contains();
  14.             }
  15.             else if(command === 'remove'){
  16.                 remove();
  17.              }
  18.              else if(command === 'shift'){
  19.                 shift();
  20.              }
  21.              else if(command === 'sumPairs'){
  22.                 sumPairs();
  23.              }
  24.              else if(command === 'print'){
  25.                 print();
  26.                 break;
  27.              }
  28.             function add(){
  29.                 let index = parseInt(splited[1]);
  30.                 let element = parseInt(splited[2]);
  31.                 arr.splice(index,0,element);
  32.             }
  33.             function addMany(){
  34.                 let index = parseInt(splited[1]);
  35.                 for(let j = splited.length-1; j >= 2 ; j--){
  36.                     let element = parseInt(splited[j]);
  37.                     arr.splice(index,0,element);
  38.                 }
  39.             }
  40.             function contains(){
  41.                 let element = parseInt(splited[1]);
  42.                     console.log(arr.indexOf(element));
  43.            }
  44.            function remove(){
  45.                  let index = parseInt(splited[1]);
  46.                  arr.splice(index,1);
  47.            }
  48.            function shift(){
  49.                 let position = parseInt(splited[1]);
  50.                 for(let a = 0; a < position; a++){
  51.                     let first = arr.shift();
  52.                     arr.push(first);
  53.                }
  54.            }
  55.              function sumPairs(){
  56.                 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);
  57.              }
  58.              function print(){
  59.                 console.log(arr);
  60.              }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement