Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arrManipulator(nums, commands){
  2.     for (let i = 0; i < commands.length; i++) {
  3.         let currentCommandArr = commands[i].split(' ');
  4.         let command = currentCommandArr[0];
  5.  
  6.         if (command === 'add') {
  7.             let index = Number(currentCommandArr[1]);
  8.             let element = Number(currentCommandArr[2]);
  9.             nums.splice(index,0,element);
  10.         } else if (command === 'addMany') {
  11.             currentCommandArr.shift()
  12.             let index = Number(currentCommandArr.shift());
  13.             currentCommandArr = currentCommandArr.map(Number);
  14.             nums.splice(index,0, ...currentCommandArr);
  15.         } else if (command === 'contains') {
  16.             let element = Number(currentCommandArr[1]);
  17.             let index = nums.indexOf(element);
  18.             console.log(index);
  19.         } else if (command === 'remove') {
  20.             let index = Number(currentCommandArr[1]);
  21.             nums.splice(index,1);
  22.         } else if (command === 'shift') {
  23.             let positions = Number(currentCommandArr[1]);
  24.             for (let r = 0; r < positions; r++) {
  25.                 let element = nums.shift();
  26.                 nums.push(element);
  27.             }
  28.         } else if (command === 'sumPairs') {
  29.             for (let p = 0; p < nums.length; p++) {
  30.                 nums[p] = nums[p] + nums[p+1];
  31.                 nums.splice(p+1,1);
  32.             }
  33.         } else if (command === 'print') {
  34.             console.log(nums);
  35.             break;
  36.         }
  37.        
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement