Advertisement
Guest User

rivision Manipulator

a guest
May 13th, 2023
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. function arrayManipulator(params, array) {
  2. let i = 0;
  3. let [command, index, element] = array[i].split(' ');
  4. index = Number(index);
  5. element = Number(element);
  6. while (command !== 'print') {
  7. switch (command) {
  8. case 'add':
  9. params.splice(index, 0, element);
  10. break;
  11. case 'addMany':
  12. let addmany = array[i].split(' ');
  13. for (let j = 2; j < addmany.length; j++) {
  14. element = Number(addmany[j]);
  15. params.splice(index, 0, element);
  16. index++
  17. }
  18. break;
  19. case 'contains':
  20. console.log(params.indexOf(index));
  21. break;
  22. case 'remove':
  23. params.splice(index, 1);
  24. break;
  25.  
  26. case 'shift':
  27. let rotations = index;
  28. for (let j = 0; j < rotations; j++) {
  29. let element = params.shift();
  30. params.push(element);
  31. }
  32. // let firstNumberGoingTotheEnd = params.splice(0, index);
  33. // for (let num of firstNumberGoingTotheEnd) {
  34. // params.push(num)
  35. //}
  36. break;
  37. case 'sumPairs':
  38. let sumArray = [];
  39. if (params.length % 2 === 0) {
  40. for (let x = 0; x < params.length; x += 2) {
  41. let pairs = params[x] + params[x + 1]
  42. sumArray.push(pairs);
  43. }
  44. } else {
  45. for (let x = 0; x < params.length - 1; x += 2) {
  46. let pairs = params[x] + params[x + 1]
  47. sumArray.push(pairs)
  48. }
  49. sumArray.push(params.pop());
  50. }
  51. params = sumArray;
  52. break;
  53. // if (params.length % 2 !== 0) {
  54. // let howMany = Math.ceil(params.length / 2);
  55. // for (let x = 0; x < howMany; x++) {
  56. // let pairs = params[0] + params[1]
  57. // params.splice(0, 2);
  58. // params.push(pairs)
  59. // }
  60. // params.unshift(params.pop());
  61. // } else {
  62. // let howMany = params.length / 2
  63. // for (let x = 0; x < howMany; x++) {
  64. // let pairs = params[0] + params[1]
  65. // params.splice(0, 2);
  66. // params.push(pairs)
  67. // }
  68. // }
  69.  
  70. }
  71. i++
  72. [command, index, element] = array[i].split(' ');
  73. index = Number(index);
  74. element = Number(element);
  75.  
  76. }
  77. return `[ ${params.join(', ')} ]`;
  78. }
  79. console.log(arrayManipulator([1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2],
  80. ["sumPairs", "sumPairs", "addMany 0 -1 -2 -3", "print"]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement