Advertisement
Guest User

Concert

a guest
Jul 19th, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function y(params = []) {
  2.     params = params.slice(0, params.indexOf('start of concert'));
  3.     let bands = {};
  4.     let playtime = {};
  5.     let totalPlaytime = 0;
  6.    
  7.     for (const line of params) {
  8.         let [command, bandName, value] = line.split('; ');
  9.  
  10.         switch (command) {
  11.             case 'Add':
  12.                 let members = value.split(', ');
  13.                 if (!bands.hasOwnProperty(bandName)) {
  14.                     bands[bandName] = [];
  15.                     for (let i = 0; i < members.length; i++) {
  16.                         if (!bands[bandName].includes(members[i])) {
  17.                             bands[bandName].push(members[i])
  18.                         }
  19.                     }
  20.                 } else {
  21.                     for (let index = 0; index < members.length; index++) {
  22.                         if (!bands[bandName].includes(members[index])) {
  23.                             bands[bandName].push(members[index])
  24.                         }
  25.                     }
  26.                 }
  27.                 break;
  28.  
  29.             default:
  30.                 let timeOnStage = +value;
  31.                 totalPlaytime += timeOnStage;
  32.                 if (!playtime.hasOwnProperty(bandName)) {
  33.                     playtime[bandName] = 0;
  34.                     playtime[bandName] += timeOnStage;
  35.                 }
  36.                 break;
  37.         }
  38.  
  39.     }
  40.  
  41.     console.log(`Total time: ${totalPlaytime}`);
  42.    
  43.     let playtimeArr = Object
  44.         .entries(playtime)
  45.         .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
  46.         .forEach(member => {
  47.             console.log(`${member[0]} -> ${member[1]}`);
  48.         });
  49.  
  50.  
  51.     let bandMembers = Object
  52.     .entries(bands)
  53.     .sort((a, b) => b[0].length - a[0].length);
  54.    
  55.     let topBand = bandMembers[0]
  56.    
  57.     console.log(topBand.shift());
  58.    
  59.     topBand[0]
  60.     .forEach(member => {
  61.         console.log(`=> ${member}`);
  62.     })
  63. }
  64. y([
  65.     'Play; The Beatles; 2584',
  66.     'Add; The Beatles; John Lennon, Paul McCartney, George Harrison, Ringo Starr',
  67.     'Add; Eagles; Glenn Frey, Don Henley, Bernie Leadon, Randy Meisner',
  68.     'Play; Eagles; 1869',
  69.     'Add; The Rolling Stones; Brian Jones, Mick Jagger, Keith Richards',
  70.     'Add; The Rolling Stones; Brian Jones, Mick Jagger, Keith Richards, Bill Wyman, Charlie Watts, Ian Stewart',
  71.     'Play; The Rolling Stones; 4239',
  72.     'start of concert',
  73.     'The Rolling Stones'
  74. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement