Advertisement
Silviya7

ThePianist

May 30th, 2024
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.     let n= Number(input.shift());
  3.      
  4.   let AllPieces={};
  5.      for (let i = 0; i < n; i++) {
  6.  
  7.         let CommandLine=input.shift();
  8.         let ArrLine1= CommandLine.split('|');
  9.         let piece=ArrLine1[0];
  10.         let composer=ArrLine1[1];
  11.         let key=ArrLine1[2];
  12.         AllPieces[piece]= {composer,key};
  13.        
  14.      }
  15.  
  16.  
  17.      let CommandLine1=input.shift();
  18.     while(CommandLine1 != 'Stop'){
  19.        let ArrLine= CommandLine1.split('|');
  20.        let piece=ArrLine[1];
  21.        switch(ArrLine[0]){
  22.         case'Add':
  23.        
  24.         const composer=ArrLine[2];
  25.         const key=ArrLine[3];
  26.        
  27.          if( AllPieces[piece] ==null){
  28.          
  29.         AllPieces[piece] ={composer,key};
  30.         console.log(`${piece} by ${composer} in ${key} added to the collection!`)
  31.        }
  32.        else{
  33.         console.log(`${piece} is already in the collection!`)
  34.        }
  35.         break;
  36.         case'Remove':
  37.          
  38.        
  39.        if( AllPieces[piece] != null){
  40.         delete  AllPieces[piece];
  41.         console.log(`Successfully removed ${piece}!`)
  42.        }
  43.        else{console.log(`Invalid operation! ${piece} does not exist in the collection.`)
  44.  
  45.        }
  46.        break;
  47.         case'ChangeKey':
  48.         let newkey=ArrLine[2];
  49.         if( AllPieces[piece] != null){
  50.             AllPieces[piece].key= newkey;
  51.             console.log(`Changed the key of ${piece} to ${newkey}!`)
  52.         }
  53.         else{
  54.             console.log(`Invalid operation! ${piece} does not exist in the collection.`);
  55.         }
  56.           break;
  57.  
  58.        }
  59.         CommandLine1=input.shift();
  60.     }
  61.  
  62.  
  63.  
  64.     for (const piece in AllPieces) {
  65.        console.log(`${piece} -> Composer: ${AllPieces[piece].composer}, Key: ${AllPieces[piece].key}`)
  66.     }
  67. }
  68.  
  69.  
  70.  
  71. solve([
  72.     '3',
  73.     'Fur Elise|Beethoven|A Minor',
  74.     'Moonlight Sonata|Beethoven|C# Minor',
  75.     'Clair de Lune|Debussy|C# Minor',
  76.     'Add|Sonata No.2|Chopin|B Minor',
  77.     'Add|Hungarian Rhapsody No.2|Liszt|C# Minor',
  78.     'Add|Fur Elise|Beethoven|C# Minor',
  79.     'Remove|Clair de Lune',
  80.     'ChangeKey|Moonlight Sonata|C# Major',
  81.     'Stop'  
  82.   ]
  83.   )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement