Advertisement
EntropyStarRover

Concert

Jul 25th, 2019
204
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function concert(input) {
  2.     let bands = [];
  3.  
  4.     let lastBand = input.pop();
  5.     let command = input.shift();
  6.  
  7.     while (command != "start of concert") {
  8.         if (command.includes("Add")) {
  9.             let [comm, bandName, bandMembers] = command.split("; ");
  10.             let bandMembersArr = bandMembers.split(", ");
  11.             let newBand = { name: bandName, members: bandMembersArr }
  12.             let bandExists = false;
  13.  
  14.             bands.forEach(band => {
  15.                 if (band["name"] == bandName) {
  16.                     bandExists = true;
  17.                     bandMembersArr.forEach(element => {
  18.                         if (band.hasOwnProperty("members")) {
  19.                             if (!band["members"].includes(element)) {
  20.                                 band["members"].push(element);
  21.                             }
  22.                         } else {
  23.                             band["members"] = bandMembersArr;
  24.                         }
  25.                     });
  26.                 }
  27.             });
  28.             if (bandExists == false) {
  29.                 bands.push(newBand);
  30.             }
  31.         } else if (command.includes("Play")) {
  32.             let [com, bandName, playtime] = command.split("; ");
  33.             playtime = Number(playtime);
  34.             let newBand = { name: bandName, time: playtime };
  35.             let bandExists = false;
  36.             bands.forEach(band => {
  37.                 if (band["name"] == bandName) {
  38.                     bandExists = true;
  39.                     if (!band.hasOwnProperty("time")) {
  40.                         band["time"] = playtime;
  41.                     } else {
  42.                         band["time"] += playtime;
  43.                     }
  44.                 }
  45.             });
  46.             if (bandExists == false) {
  47.                 bands.push(newBand);
  48.             }
  49.         }
  50.         command = input.shift();
  51.     }
  52.     let sorted = bands.sort((a, b) => {if (a["time"] !== b["time"]) {
  53.         return b["time"] - a["time"]
  54.         } else {
  55.         return a["name"].localeCompare(b["name"]);
  56.         }});
  57.  
  58.     let totalTime = 0;
  59.     bands.forEach(band => {
  60.         totalTime += band["time"];
  61.     });
  62.     console.log(`Total time: ${totalTime}`);
  63.  
  64.     sorted.forEach(element => {
  65.         console.log(`${element["name"]} -> ${element["time"]}`);
  66.     });
  67.     console.log(lastBand);
  68.     sorted.forEach(element => {
  69.         if (element["name"] == lastBand) {
  70.             let membersArr = element["members"];
  71.             membersArr.forEach(member => {
  72.                 console.log(`=> ${member}`);
  73.             });
  74.         }
  75.     });
  76. }
Advertisement
RAW Paste Data Copied
Advertisement