Advertisement
Spocoman

08. Array Manipulations

Feb 6th, 2022
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arrayManipulations(input) {
  2.     let arr = input.shift().split(' ').map(Number);
  3.  
  4.     while (input.length !== 0) {
  5.         let command = input.shift().split(' ');
  6.  
  7.         if (command[0] === 'Add') {
  8.             add(Number(command[1]));
  9.         } else if (command[0] === 'Remove') {
  10.             remove(Number(command[1]));
  11.         } else if (command[0] === 'RemoveAt') {
  12.             removeAt(Number(command[1]));
  13.         } else if (command[0] === 'Insert') {
  14.             insert(Number(command[1]), Number(command[2]));
  15.         }
  16.     }
  17.  
  18.     function add(num) {
  19.         arr.push(Number(num));
  20.     }
  21.  
  22.     function remove(num) {
  23.         arr = arr.filter(x => x !== num);
  24.     }
  25.  
  26.     function removeAt(num) {
  27.         arr.splice(num, 1);
  28.     }
  29.  
  30.     function insert(num, i) {
  31.         arr.splice(i, 0, num);
  32.     }
  33.  
  34.     console.log(arr.join(' '));
  35.  
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement