Liliana797979

task 2.1 - mid exam

Jul 22nd, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. function sequence(array) {
  3.     let list = array.shift().split(' ').map(Number);
  4.     let listLength = list.length
  5.     for (let command of array) {
  6.         let token = command.split(' ');
  7.  
  8.         if (token[0] === 'Add') {
  9.             list.push(Number(token[1]));
  10.         }
  11.         else if (token[0] === 'Remove') {
  12.             let value = Number(token[1]);
  13.             if (list.includes(value)) {
  14.                 list.splice(list.indexOf(value), 1);
  15.  
  16.             }
  17.         } else if (token[0] === 'Replace') {
  18.             let value = Number(token[1]);
  19.             let replacement = Number(token[2]);
  20.             if (list.includes(value)) {
  21.                 list.splice(list.indexOf(value), 1, replacement);
  22.             }
  23.         } else if (token[0] === 'Collapse') {
  24.             let value = Number(token[1]);
  25.  
  26.             for (let i = 0; i < listLength; i++) {
  27.  
  28.                 if (list[i] < value) {
  29.                     list.splice(list.indexOf(list[i]), 1)
  30.                 }
  31.             }
  32.         } else if (token[0] === 'Finish') {
  33.             console.log(list.join(' '));
  34.         }
  35.  
  36.     }
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment