Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function manipulator(arr, input) {
  2.     let arrToPrint = arr.slice();
  3.  
  4.     for (let i = 0; i < input.length; i++) {
  5.         let command = input[i].split(' ');
  6.         let action = command[0];
  7.  
  8.         if (action === 'add') {
  9.             let ind = Number(command[1]);
  10.             let el = Number(command[2]);
  11.  
  12.             addCommand(ind, el);
  13.         }
  14.         else if (action === 'addMany') {
  15.             let ind = Number(command[1]);
  16.  
  17.             for (let i = command.length - 1; i >= 2; i--) {
  18.                 let element = Number(command[i]);
  19.                 arrToPrint.splice(ind,0,element);
  20.             }
  21.         }
  22.         else if (action === 'contains') {
  23.             let el = Number(command[1]);
  24.  
  25.             isContain(el);
  26.         }
  27.         else if (action === 'remove') {
  28.             let ind = Number(command[1]);
  29.  
  30.             arrToPrint.splice(ind, 1);
  31.         }
  32.         else if (action === 'shift') {
  33.             let positions = Number(command[1]);
  34.  
  35.             while (positions > 0) {
  36.                 arrToPrint.push(arrToPrint.shift());
  37.                 positions--;
  38.             }
  39.         }
  40.         else if (action === 'sumPairs') {
  41.             let testArr = arrToPrint.slice();
  42.                 arrToPrint = [];
  43.             while (testArr.length > 1) {
  44.                 let num = testArr.shift() + testArr.shift();
  45.                 arrToPrint.push(num);
  46.             }
  47.             if (testArr.length === 1) {
  48.                 arrToPrint.push(testArr.shift());
  49.             }
  50.         }
  51.         else if (action === 'print') {
  52.             console.log(`[ ${arrToPrint.join(', ')} ]`);
  53.             break;
  54.         }
  55.     }
  56.  
  57.     function isContain(num) {
  58.         if (arrToPrint.indexOf(num) !== -1) {
  59.             console.log(arrToPrint.indexOf(num));
  60.         }
  61.         else {
  62.             console.log(-1);
  63.         }
  64.     }
  65.  
  66.     function addCommand(index, element) {
  67.         arrToPrint.splice(index, 0, element);
  68.     }
  69.  
  70.    
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement