Advertisement
viligen

Pianist_JS

May 8th, 2022
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let commands = {
  3.         Add,
  4.         Remove,
  5.         ChangeKey
  6.     }
  7.     let allPieces = {}
  8.     let number = Number(input.shift())
  9.     for (let i = 0; i < number; i++) {
  10.         let [newPiece, composer, key] = input.shift().split("|")
  11.         allPieces[newPiece] = {
  12.             composer,
  13.             key
  14.         }
  15.     }
  16.     while (input.length > 0) {
  17.         let [command, ...params] = input.shift().split('|')
  18.         if (command === 'Stop') {
  19.             break
  20.         }
  21.         commands[command](allPieces, ...params)
  22.  
  23.  
  24.     }
  25.     //"{Piece} -> Composer: {composer}, Key: {key}"
  26.     listed = Object
  27.         .keys(allPieces)
  28.         .sort((a, b) => allPieces[a].composer.localeCompare(allPieces[b].composer))
  29.     for (piece of listed) {
  30.         console.log(`${piece} -> Composer: ${allPieces[piece].composer}, Key: ${allPieces[piece].key}`)
  31.     }
  32.  
  33.  
  34.  
  35.  
  36.     function Add(allPieces, piece, composer, key) {
  37.         if (allPieces.hasOwnProperty(piece)) {
  38.             console.log(`${piece} is already in the collection!`)
  39.             return
  40.         }
  41.         allPieces[piece] = {
  42.             composer,
  43.             key
  44.         }
  45.         console.log(`${piece} by ${composer} in ${key} added to the collection!`)
  46.     }
  47.  
  48.     function Remove(allPieces, piece) {
  49.         if (!allPieces.hasOwnProperty(piece)) {
  50.             console.log(`Invalid operation! ${piece} does not exist in the collection.`)
  51.             return
  52.         }
  53.         delete allPieces[piece]
  54.         console.log(`Successfully removed ${piece}!`)
  55.  
  56.     }
  57.  
  58.     function ChangeKey(allPieces, piece, newKey) {
  59.         if (!allPieces.hasOwnProperty(piece)) {
  60.             console.log(`Invalid operation! ${piece} does not exist in the collection.`)
  61.             return
  62.         }
  63.         allPieces[piece].key = newKey
  64.         console.log(`Changed the key of ${piece} to ${newKey}!`)
  65.     }
  66.  
  67.  
  68. }
  69.  
  70. solve([
  71.     '4',
  72.     'Eine kleine Nachtmusik|Mozart|G Major',
  73.     'La Campanella|Liszt|G# Minor',
  74.     'The Marriage of Figaro|Mozart|G Major',
  75.     'Hungarian Dance No.5|Brahms|G Minor',
  76.     'Add|Spring|Vivaldi|E Major',
  77.     'Remove|The Marriage of Figaro',
  78.     'Remove|Turkish March',
  79.     'ChangeKey|Spring|C Major',
  80.     'Add|Nocturne|Chopin|C# Minor',
  81.     'Stop'
  82. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement