Advertisement
Todorov_Stanimir

01. Concert Fundamentals Final Exam Prepar. - 24 July 2019

Jul 24th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function concert(input) {
  2.     let bands = {};
  3.     let totalTime = 0;
  4.  
  5.     while ((line = input.shift()) !== 'start of concert') {
  6.         let [command, name, membersOrTime] = line.split('; ');
  7.         if (command === 'Add') {
  8.             addband(name);
  9.             membersOrTime.split(', ').forEach(member => {
  10.                 if (!bands[name].members.includes(member)) {
  11.                     bands[name].members.push(member);
  12.                 }
  13.             });
  14.         } else {
  15.             let time = Number(membersOrTime);
  16.             addband(name);
  17.             bands[name].time += time;
  18.             totalTime += time;
  19.         }
  20.     }
  21.     console.log(`Total time: ${totalTime}`);
  22.     Object.entries(bands)
  23.         .sort((a, b) => b[1].time - a[1].time || a[0].localeCompare(b[0]))
  24.         .forEach(band => console.log(`${band[0]} -> ${band[1].time}`));
  25.  
  26.     let bandName = input.shift();
  27.     console.log(bandName);
  28.     Object.values(bands[bandName])[1].forEach(member => console.log(`=> ${member}`));
  29.  
  30.     function addband(name) {
  31.         if (!bands[name]) {
  32.             bands[name] = {
  33.                 time: 0,
  34.                 members: []
  35.             }
  36.         }
  37.     }
  38. }
  39. concert(['Play; The Beatles; 2584',
  40.     'Add; The Beatles; John Lennon, Paul McCartney, George Harrison, Ringo Starr',
  41.     'Add; Eagles; Glenn Frey, Don Henley, Bernie Leadon, Randy Meisner',
  42.     'Play; Eagles; 1869',
  43.     'Add; The Rolling Stones; Brian Jones, Mick Jagger, Keith Richards',
  44.     'Add; The Rolling Stones; Brian Jones, Mick Jagger, Keith Richards, Bill Wyman, Charlie Watts, Ian Stewart',
  45.     'Play; The Rolling Stones; 4239',
  46.     'start of concert',
  47.     'The Rolling Stones']);
  48. concert(['Add; The Beatles; John Lennon, Paul McCartney',
  49.     'Add; The Beatles; Paul McCartney, George Harrison',
  50.     'Add; The Beatles; George Harrison, Ringo Starr',
  51.     'Play; The Beatles; 3698',
  52.     'Play; The Beatles; 3828',
  53.     'start of concert',
  54.     'The Beatles']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement