Advertisement
Spocoman

02. SoftUni Karaoke

Feb 16th, 2024
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function softUniKaraoke(input) {
  2.     let participants = input.shift().split(", "),
  3.         songs = input.shift().split(", "),
  4.         info = {};
  5.  
  6.     while (true) {
  7.         let command = input.shift();
  8.         if (command === "dawn") {
  9.             break;
  10.         }
  11.  
  12.         let [participant, song, award] = command.split(", ");
  13.  
  14.         if (participants.includes(participant) && songs.includes(song)) {
  15.             if (!info.hasOwnProperty(participant)) {
  16.                 info[participant] = new Set();
  17.             }
  18.             info[participant].add(award);
  19.         }
  20.     }
  21.  
  22.     info = Object.fromEntries(
  23.         Object.entries(info).sort((a, b) => b[1].size - a[1].size)
  24.     );
  25.  
  26.     if (Object.keys(info).length > 0) {
  27.         for (let key in info) {
  28.             console.log(`${key}: ${info[key].size} awards`);
  29.             for (let value of Array.from(info[key]).sort()) {
  30.                 console.log(`--${value}`);
  31.             }
  32.         }
  33.     } else {
  34.         console.log("No awards");
  35.     }
  36.  
  37.     return;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement