Advertisement
PowerCell46

Array manipulation

Dec 1st, 2022
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arrayManipulator(array, commands) {
  2.  
  3.     let newArray = array.slice();
  4.     let sum = null;
  5.  
  6.     for (let index = 0; index < Number(commands.length); index++) {
  7.         let sumArray = [];
  8.         let currentCommand = commands[index];
  9.         currentCommand = currentCommand.split(" ");
  10.  
  11.         if (currentCommand[0] === "add") {
  12.             newArray.splice(Number(currentCommand[1]), 0, Number(currentCommand[2]));
  13.             continue;
  14.         }
  15.  
  16.         if (currentCommand[0] === "addMany") {
  17.             let index2 = Number(currentCommand.length) - 1;
  18.             while (index2 > 1) {
  19.                 newArray.splice(Number(currentCommand[1]), 0, Number(currentCommand[index2]));
  20.                 index2--;
  21.             }
  22.             continue;
  23.         }
  24.  
  25.         if (currentCommand[0] === "contains") {
  26.             let searchedNum = Number(currentCommand[1]);
  27.             console.log(newArray.indexOf(searchedNum));
  28.             continue;
  29.         }
  30.  
  31.         if (currentCommand[0] === "remove") {
  32.             newArray.splice(Number(currentCommand[1]), 1);
  33.             continue;
  34.         }
  35.  
  36.         if (currentCommand[0] === "shift") {
  37.             let numberOfShifts = Number(currentCommand[1]);
  38.             while (numberOfShifts > 0) {
  39.                 let currentShift = newArray.shift();
  40.                 newArray.push(currentShift);
  41.                 numberOfShifts--;
  42.             }
  43.         }
  44.  
  45.         if (currentCommand[0] === "sumPairs") {
  46.             let index3 = 0;
  47.             if (Number(newArray.length) % 2 === 0) {
  48.                 while (index3 < Number(newArray.length)) {
  49.                     let currentNum = Number(newArray[index3]);
  50.                     index3++;
  51.                     let nextNum = Number(newArray[index3]);
  52.                     sum = currentNum + nextNum;
  53.                     sumArray.push(sum);
  54.                     index3++;
  55.                 }
  56.                 newArray = sumArray;
  57.                 continue;
  58.             } else {
  59.                 while (index3 < Number(newArray.length - 1)) {
  60.                     let currentNum = Number(newArray[index3]);
  61.                     index3++;
  62.                     let nextNum = Number(newArray[index3]);
  63.                     sum = currentNum + nextNum;
  64.                     sumArray.push(sum);
  65.                     index3++;
  66.                 }
  67.                 sum = newArray[index3]
  68.                 sumArray.push(sum);
  69.                 newArray = sumArray;
  70.                 continue;
  71.             }
  72.         }
  73.        
  74.         if (currentCommand[0] === "print") {
  75.             console.log(`[ ${newArray.join(", ")} ]`);
  76.             break;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement