Advertisement
vladovip

JS FUND FINEX_The Pianists

May 8th, 2022
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function thePianist(inputArr) {
  2.   let numOfPieces = +inputArr.shift();
  3.   let recordsPieceObj = {};
  4.   let counter = 1;
  5.   while (counter <= numOfPieces) {
  6.     let pieceLine = inputArr.shift();
  7.     let tokens = pieceLine.split("|");
  8.     let piece = tokens[0];
  9.     let composer = tokens[1];
  10.     let keyPiece = tokens[2];
  11.     //  console.log(`piece : ${piece} ; composer: ${composer}; key: ${keyPiece}`);
  12.     if (recordsPieceObj.hasOwnProperty(piece) == false) {
  13.       recordsPieceObj[piece] = {
  14.         songComposer: composer,
  15.         pieceDetails: keyPiece,
  16.       };
  17.     } else {
  18.       continue;
  19.     }
  20.     counter++;
  21.   }
  22.   // console.log(recordsPieceObj);
  23.  
  24.   let commandLine = inputArr.shift();
  25.  
  26.   while (commandLine !== "Stop") {
  27.     let infoLine = commandLine.split("|");
  28.     let currentCommand = infoLine[0];
  29.  
  30.     if (currentCommand == "Add") {
  31.       let currentPiece = infoLine[1];
  32.       let currentComposer = infoLine[2];
  33.       let currentKey = infoLine[3];
  34.       if (recordsPieceObj.hasOwnProperty(currentPiece) == false) {
  35.         recordsPieceObj[currentPiece] = {
  36.           songComposer: currentComposer,
  37.           pieceDetails: currentKey,
  38.         };
  39.         console.log(
  40.           `${currentPiece} by ${currentComposer} in ${currentKey} added to the collection!`
  41.         );
  42.       } else if (recordsPieceObj.hasOwnProperty(currentPiece) == true) {
  43.         console.log(`${currentPiece} is already in the collection!`);
  44.       }
  45.     }
  46.  
  47.     if (currentCommand == "Remove") {
  48.       let pieceToRemove = infoLine[1];
  49.       if (recordsPieceObj.hasOwnProperty(pieceToRemove) == true) {
  50.         console.log(`Successfully removed ${pieceToRemove}!`);
  51.         delete recordsPieceObj[pieceToRemove];
  52.       } else {
  53.         console.log(
  54.           `Invalid operation! ${pieceToRemove} does not exist in the collection.`
  55.         );
  56.       }
  57.     }
  58.  
  59.     if (currentCommand == "ChangeKey") {
  60.       let currentPiece = infoLine[1];
  61.       let newKey = infoLine[2];
  62.       if (recordsPieceObj.hasOwnProperty(currentPiece) == true) {
  63.         recordsPieceObj[currentPiece].pieceDetails = newKey;
  64.         console.log(`Changed the key of ${currentPiece} to ${newKey}!`);
  65.       } else {
  66.         console.log(
  67.           `Invalid operation! ${currentPiece} does not exist in the collection.`
  68.         );
  69.       }
  70.     }
  71.  
  72.     commandLine = inputArr.shift();
  73.   }
  74.  
  75.   for (let element in recordsPieceObj) {
  76.     console.log(
  77.       `${element} -> Composer: ${recordsPieceObj[element].songComposer}, Key: ${recordsPieceObj[element].pieceDetails}`
  78.     );
  79.   }
  80. }
  81.  
  82.  
  83.  
  84. thePianist([
  85.   "3",
  86.   "Fur Elise|Beethoven|A Minor",
  87.   "Moonlight Sonata|Beethoven|C# Minor",
  88.   "Clair de Lune|Debussy|C# Minor",
  89.   "Add|Sonata No.2|Chopin|B Minor",
  90.   "Add|Hungarian Rhapsody No.2|Liszt|C# Minor",
  91.   "Add|Fur Elise|Beethoven|C# Minor",
  92.   "Remove|Clair de Lune",
  93.   "ChangeKey|Moonlight Sonata|C# Major",
  94.   "Stop",
  95. ]);
  96.  
  97. console.log(`*********`);
  98.  
  99. thePianist([
  100.   "4",
  101.   "Eine kleine Nachtmusik|Mozart|G Major",
  102.   "La Campanella|Liszt|G# Minor",
  103.   "The Marriage of Figaro|Mozart|G Major",
  104.   "Hungarian Dance No.5|Brahms|G Minor",
  105.   "Add|Spring|Vivaldi|E Major",
  106.   "Remove|The Marriage of Figaro",
  107.   "Remove|Turkish March",
  108.   "ChangeKey|Spring|C Major",
  109.   "Add|Nocturne|Chopin|C# Minor",
  110.   "Stop",
  111. ]);
  112.  
  113. console.log(`**********`);
  114.  
  115. // 100 points in the judge system
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement