Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function thePianist(input) {
- let initialPieces = input.shift();
- let collectionOfPieces = [];
- let pieceInTheCollectionFlag = false;
- for (let index = 0; index < initialPieces; index++) {
- let currentPiece = input[index].split("|");
- let [piece, composer, key] = currentPiece;
- let pieceObj = { piece, composer, key };
- collectionOfPieces.push(pieceObj);
- }
- let newInput = input.slice(initialPieces, input.length);
- let command = newInput.shift();
- while (command != "Stop") {
- let commandParts = command.split("|");
- let typeOfCommand = commandParts[0];
- switch (typeOfCommand) {
- case "Add":
- let [_typeOfCommand, piece, composer, key] = commandParts;
- let currentPieceObj = { piece, composer, key };
- for (const obj of collectionOfPieces) {
- if (obj.piece === piece) {
- console.log(`${piece} is already in the collection!`);
- pieceInTheCollectionFlag = true;
- }
- }
- if (!pieceInTheCollectionFlag) {
- console.log(`${piece} by ${composer} in ${key} added to the collection!`)
- collectionOfPieces.push(currentPieceObj);
- }
- pieceInTheCollectionFlag = false;
- break;
- case "Remove":
- let pieceForRemove = commandParts[1];
- let indexToRemove = -1;
- for (let i = 0; i < collectionOfPieces.length; i++) {
- if (collectionOfPieces[i].piece === pieceForRemove) {
- console.log(`Successfully removed ${pieceForRemove}!`);
- indexToRemove = i;
- break;
- }
- }
- if (indexToRemove !== -1) {
- collectionOfPieces.splice(indexToRemove, 1);
- } else {
- console.log(`Invalid operation! ${pieceForRemove} does not exist in the collection.`);
- }
- break;
- case "ChangeKey":
- let pieceToChangeKey = commandParts[1];
- let newKey = commandParts[2];
- for (const obj of collectionOfPieces) {
- if (obj.piece === pieceToChangeKey) {
- console.log(`Changed the key of ${pieceToChangeKey} to ${newKey}!`);
- obj.key = newKey;
- pieceInTheCollectionFlag = true;
- }
- }
- if (!pieceInTheCollectionFlag) {
- console.log(`Invalid operation! ${pieceToChangeKey} does not exist in the collection.`)
- }
- pieceInTheCollectionFlag = false;
- break;
- }
- command = newInput.shift();
- }
- for (const pieceObject of collectionOfPieces) {
- console.log(`${pieceObject.piece} -> Composer: ${pieceObject.composer}, Key: ${pieceObject.key}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement