Advertisement
knoteva

Untitled

Oct 18th, 2019
1,194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(commands){
  2.     let arr = commands
  3.     .shift()
  4.     .split(' ')
  5.     .map(Number);
  6.     for(let i = 0;i<commands.length;i++){ // въртим по броя на командите
  7.         let [command,firstNum, secondNum]=commands[i].split(' '); // взимаме съответната команда, число и индекс. П.С. Първо бих взела само командата и само, когато тя е insert ще взимам secondNum(който реално е индекса)
  8.         firstNum = Number(firstNum);
  9.         secondNum = Number(secondNum);
  10.  
  11.    
  12.     switch(command){
  13.         case "Add":
  14.             add(firstNum); // подаваме параметър
  15.             break;
  16.             case "Remove":
  17.                 remove(firstNum); // подаваме параметър
  18.                 break;
  19.                 case "RemoveAt":
  20.                         removeAt(firstNum); //подаваме параметър
  21.                     break;
  22.                     case "Insert":
  23.                     insert(firstNum, secondNum); // подаваме параметри
  24.                         break;
  25.     }
  26. }
  27.     function add(el){
  28.         arr.push(el)
  29.         }
  30.         function remove(num){
  31.             arr=arr.filter(el=>el!==num);
  32.         }
  33.         function removeAt(index){
  34.             arr.splice(index,1);
  35.         }
  36.         function insert(num,index){
  37.             arr.splice(index,0,num);
  38.         }
  39.         console.log(arr.join(' '))
  40. }
  41.  
  42. solve([ '4 19 2 53 6 43', 'Add 3', 'Remove 2', 'RemoveAt 1', 'Insert 8 3' ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement