Liliana797979

problem 2_3 - mid exam

Jul 22nd, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.      
  3. function solve(input) {
  4.  
  5.     let oldFavoriteGenres = input.shift().split(' | ');
  6.     let line = input.shift();
  7.  
  8.     while (line != "Stop!") {
  9.         let [command, item, newItem] = line.split(' ');
  10.  
  11.         switch (command) {
  12.             case 'Join':
  13.                 toJoin(oldFavoriteGenres, item);
  14.                 break;
  15.             case 'Drop':
  16.                 drop(oldFavoriteGenres, item);
  17.                 break;
  18.             case 'Replace':
  19.                 replace(oldFavoriteGenres, item, newItem);
  20.                 break;
  21.             default:
  22.                 break;
  23.         }
  24.         line = input.shift();
  25.     }
  26.  
  27.  
  28.     console.log(oldFavoriteGenres.join(' '));
  29.  
  30.     function toJoin(list, item) {
  31.         if (list.includes(item) == false) {
  32.             list.push(item);
  33.         }
  34.     }
  35.  
  36.     function drop(list, item) {
  37.         if (list.includes(item) == true) {
  38.             let index = list.indexOf(item);
  39.             list.splice(index, 1);
  40.         }
  41.  
  42.     }
  43.  
  44.     function replace(list, item, newItem) {
  45.         if (list.includes(item) == true) {
  46.             let index = list.indexOf(item);
  47.             list[index] = newItem;
  48.         }
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment