Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(args) {
- const numberOfPieces = Number(args.shift());
- const pieces = {};
- for (let i = 0; i < numberOfPieces; i++) {
- const element = args[i];
- const [piece, composer, key] = element.split('|');
- pieces[piece] = { composer, key };
- }
- let lineInput = args.shift();
- while (lineInput !== 'Stop') {
- const [command, piece, composer, key] = lineInput.split('|');
- switch (command) {
- case 'Add':
- if (Object.prototype.hasOwnProperty.call(pieces, piece)) {
- console.log(`${piece} is already in the collection!`);
- } else {
- pieces[piece] = { composer, key };
- console.log(`${piece} by ${composer} in ${key} added to the collection!`);
- }
- break;
- case 'Remove':
- if (Object.prototype.hasOwnProperty.call(pieces, piece)) {
- delete pieces[piece];
- console.log(`Successfully removed ${piece}!`);
- } else {
- console.log(`Invalid operation! ${piece} does not exist in the collection.`);
- }
- break;
- case 'ChangeKey':
- if (Object.prototype.hasOwnProperty.call(pieces, piece)) {
- pieces[piece].key = composer;
- console.log(`Changed the key of ${piece} to ${composer}!`);
- } else {
- console.log(`Invalid operation! ${piece} does not exist in the collection.`);
- }
- break;
- default:
- break;
- }
- lineInput = args.shift();
- }
- Object.entries(pieces)
- .sort((a, b) => a[0].localeCompare(b[0]) || a[1].composer.localeCompare(b[1].composer))
- .forEach((el) => {
- const [piece, props] = el;
- const { composer, key } = props;
- console.log(`${piece} -> Composer: ${composer}, Key: ${key}`);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement