Advertisement
GalinaKG

Untitled

Mar 27th, 2023
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function thePianist(input) {
  2.     let initialPieces = input.shift();
  3.     let collectionOfPieces = [];
  4.     let pieceInTheCollectionFlag = false;
  5.  
  6.     for (let index = 0; index < initialPieces; index++) {
  7.         let currentPiece = input[index].split("|");
  8.         let [piece, composer, key] = currentPiece;
  9.  
  10.         let pieceObj = { piece, composer, key };
  11.         collectionOfPieces.push(pieceObj);
  12.     }
  13.  
  14.     let newInput = input.slice(initialPieces, input.length);
  15.  
  16.     let command = newInput.shift();
  17.  
  18.     while (command != "Stop") {
  19.         let commandParts = command.split("|");
  20.         let typeOfCommand = commandParts[0];
  21.  
  22.         switch (typeOfCommand) {
  23.             case "Add":
  24.                 let [_typeOfCommand, piece, composer, key] = commandParts;
  25.                 let currentPieceObj = { piece, composer, key };
  26.  
  27.                 for (const obj of collectionOfPieces) {
  28.                     if (obj.piece === piece) {
  29.                         console.log(`${piece} is already in the collection!`);
  30.                         pieceInTheCollectionFlag = true;
  31.                     }
  32.                 }
  33.                 if (!pieceInTheCollectionFlag) {
  34.                     console.log(`${piece} by ${composer} in ${key} added to the collection!`)
  35.                     collectionOfPieces.push(currentPieceObj);
  36.                 }
  37.                 pieceInTheCollectionFlag = false;
  38.                 break;
  39.             case "Remove":
  40.                 let pieceForRemove = commandParts[1];
  41.                 let indexToRemove = -1;
  42.  
  43.                 for (let i = 0; i < collectionOfPieces.length; i++) {
  44.                     if (collectionOfPieces[i].piece === pieceForRemove) {
  45.                         console.log(`Successfully removed ${pieceForRemove}!`);
  46.                         indexToRemove = i;
  47.                         break;
  48.                     }
  49.                 }
  50.  
  51.                 if (indexToRemove !== -1) {
  52.                     collectionOfPieces.splice(indexToRemove, 1);
  53.                 } else {
  54.                     console.log(`Invalid operation! ${pieceForRemove} does not exist in the collection.`);
  55.                 }
  56.                 break;
  57.             case "ChangeKey":
  58.                 let pieceToChangeKey = commandParts[1];
  59.                 let newKey = commandParts[2];
  60.  
  61.                 for (const obj of collectionOfPieces) {
  62.                     if (obj.piece === pieceToChangeKey) {
  63.                         console.log(`Changed the key of ${pieceToChangeKey} to ${newKey}!`);
  64.                         obj.key = newKey;
  65.                         pieceInTheCollectionFlag = true;
  66.                     }
  67.                 }
  68.                 if (!pieceInTheCollectionFlag) {
  69.                     console.log(`Invalid operation! ${pieceToChangeKey} does not exist in the collection.`)
  70.                 }
  71.                 pieceInTheCollectionFlag = false;
  72.                 break;
  73.         }
  74.  
  75.         command = newInput.shift();
  76.     }
  77.  
  78.     for (const pieceObject of collectionOfPieces) {
  79.         console.log(`${pieceObject.piece} -> Composer: ${pieceObject.composer}, Key: ${pieceObject.key}`);
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement