Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let commands = {
- Add,
- Remove,
- ChangeKey
- }
- let allPieces = {}
- let number = Number(input.shift())
- for (let i = 0; i < number; i++) {
- let [newPiece, composer, key] = input.shift().split("|")
- allPieces[newPiece] = {
- composer,
- key
- }
- }
- while (input.length > 0) {
- let [command, ...params] = input.shift().split('|')
- if (command === 'Stop') {
- break
- }
- commands[command](allPieces, ...params)
- }
- //"{Piece} -> Composer: {composer}, Key: {key}"
- listed = Object
- .keys(allPieces)
- .sort((a, b) => allPieces[a].composer.localeCompare(allPieces[b].composer))
- for (piece of listed) {
- console.log(`${piece} -> Composer: ${allPieces[piece].composer}, Key: ${allPieces[piece].key}`)
- }
- function Add(allPieces, piece, composer, key) {
- if (allPieces.hasOwnProperty(piece)) {
- console.log(`${piece} is already in the collection!`)
- return
- }
- allPieces[piece] = {
- composer,
- key
- }
- console.log(`${piece} by ${composer} in ${key} added to the collection!`)
- }
- function Remove(allPieces, piece) {
- if (!allPieces.hasOwnProperty(piece)) {
- console.log(`Invalid operation! ${piece} does not exist in the collection.`)
- return
- }
- delete allPieces[piece]
- console.log(`Successfully removed ${piece}!`)
- }
- function ChangeKey(allPieces, piece, newKey) {
- if (!allPieces.hasOwnProperty(piece)) {
- console.log(`Invalid operation! ${piece} does not exist in the collection.`)
- return
- }
- allPieces[piece].key = newKey
- console.log(`Changed the key of ${piece} to ${newKey}!`)
- }
- }
- solve([
- '4',
- 'Eine kleine Nachtmusik|Mozart|G Major',
- 'La Campanella|Liszt|G# Minor',
- 'The Marriage of Figaro|Mozart|G Major',
- 'Hungarian Dance No.5|Brahms|G Minor',
- 'Add|Spring|Vivaldi|E Major',
- 'Remove|The Marriage of Figaro',
- 'Remove|Turkish March',
- 'ChangeKey|Spring|C Major',
- 'Add|Nocturne|Chopin|C# Minor',
- 'Stop'
- ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement