Liliana797979

2_numbers - mid exam - fundamentals

Aug 16th, 2021
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr) {
  2.     let list = arr.shift().split(' ').map(Number);
  3.     for(let element of arr) {
  4.         let temp = element.split(' ');
  5.         let command = temp[0];
  6.         let next = Number(temp[1]);
  7.         if(command === "Add") {
  8.             list.push(next);
  9.         }
  10.         else if(command === "Remove") {
  11.             list.splice(list.indexOf(next), 1);
  12.         }
  13.         else if(command === "Replace") {
  14.             let random = Number(temp[2]);
  15.             list.splice(list.indexOf(next), 1, random);
  16.         }
  17.         else if(command === "Collapse") {
  18.             let fix = list.filter(x => x >= next);
  19.             list.splice(0, list.length);
  20.             list.push(...fix);
  21.         }
  22.         else if(command === "Finish") {
  23.             break;
  24.         }
  25.     }
  26.     console.log(list.join(' '));
  27. }
  28.  
  29. solve(["5 9 70 -56 9 9","Replace 9 10","Remove 9","Finish"]);
Advertisement
Add Comment
Please, Sign In to add comment