Advertisement
dddilian

08. Array Manipulator - Lab

Jun 19th, 2020
1,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let arr = input.shift().split(' ').map(Number);
  4.  
  5.     for (let i = 0; i < input.length; i++) {
  6.  
  7.         let command = input[i].split(' ');
  8.         //console.log(command)
  9.         let word = command[0];
  10.  
  11.         if (word == 'Add') {
  12.             let num1 = Number(command[1]);
  13.             arr.push(num1);
  14.         } else if (word == 'Remove') {
  15.             let num2 = Number(command[1]);
  16.             for (let j = 0; j < arr.length; j++) {
  17.                 if (arr[j] == num2) {
  18.                     arr.splice(j, 1);
  19.                 }
  20.             }
  21.         } else if (word == 'RemoveAt') {
  22.             let num3 = Number(command[1]);
  23.             arr.splice(num3, 1);
  24.         } else if (word == 'Insert') {
  25.             let num4 = Number(command[1]);
  26.             let index = Number(command[2]);
  27.             arr.splice(index, 0, num4);
  28.         }
  29.     }
  30.  
  31.  
  32.     console.log(arr.join(' '));
  33.  
  34. }
  35.  
  36. solve(['4 19 2 53 6 43',
  37.     'Add 3',
  38.     'Remove 2',
  39.     'RemoveAt 1',
  40.     'Insert 8 3'
  41. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement