Liliana797979

problem 2_2 - mid exam

Jul 22nd, 2021
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.      
  3. function solve(input) {
  4.  
  5.     let favoriteGenres = input.shift().split(' | ');
  6.     let line = input.shift();
  7.  
  8.     while (line != 'Stop!') {
  9.         let [command, genre, newGenre] = line.split(' ');
  10.  
  11.         switch (command) {
  12.             case 'Join':
  13.                 if (favoriteGenres.includes(genre) == false) {
  14.                     favoriteGenres.push(genre);
  15.                 }
  16.                 break;
  17.             case 'Drop':
  18.                 if (favoriteGenres.includes(genre) == true) {
  19.                     let index = favoriteGenres.indexOf(genre);
  20.                     favoriteGenres.splice(index, 1);
  21.                 }
  22.                 break;
  23.             case 'Replace':
  24.                 if (favoriteGenres.includes(genre) == true) {
  25.                     let index = favoriteGenres.indexOf(genre);
  26.                     favoriteGenres[index] = newGenre;
  27.                 }
  28.                 break;
  29.             default:
  30.                 break;
  31.         }
  32.  
  33.  
  34.         line = input.shift();
  35.     }
  36.     console.log(favoriteGenres.join(' '));
  37. }
Advertisement
Add Comment
Please, Sign In to add comment