Advertisement
vladovip

Concert JS FUND FINEX V1

Aug 18th, 2022
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function concert(inputArr) {
  2.   let AllBands = {};
  3.   let commandLine = inputArr.shift();
  4.  
  5.   while (commandLine !== "Start!") {
  6.     let tokens = commandLine.split("; ");
  7.     let command = tokens[0];
  8.     // console.log(command);
  9.     // console.log(bandName);
  10.     let bandName = tokens[1];
  11.     let parametars = tokens[2];
  12.  
  13.     if (command == "Add") {
  14.       let addMembersArr = parametars.split(", ");
  15.       if (AllBands.hasOwnProperty(bandName) == false) {
  16.         AllBands[bandName] = { bandMembers: [], time: 0 };
  17.         for (let member of addMembersArr) {
  18.           AllBands[bandName].bandMembers.push(member);
  19.         }
  20.       } else if (AllBands.hasOwnProperty(bandName) == true) {
  21.         for (let member of addMembersArr) {
  22.           if (AllBands[bandName].bandMembers.includes(member) == false) {
  23.             AllBands[bandName].bandMembers.push(member);
  24.           }
  25.         }
  26.       }
  27.     } else if ( command == "Play" ){
  28.          let playTime = Number(parametars);
  29.          if( AllBands.hasOwnProperty(bandName) == false){
  30.             AllBands[bandName] = { bandMembers: [], time: 0 };
  31.             AllBands[bandName].time += playTime;
  32.         } else if ( AllBands.hasOwnProperty(bandName) == true ){
  33.              AllBands[bandName].time += playTime;
  34.         }
  35.     }
  36.     commandLine = inputArr.shift();
  37.   }
  38.  
  39.      let sumTime = 0;
  40.      for ( let bandName in AllBands ){
  41.           sumTime += AllBands[bandName].time;
  42.      }
  43.      console.log(`Total time: ${sumTime}`);
  44.  
  45.      for (let bandName in AllBands) {
  46.       console.log(`${bandName} -> ${AllBands[bandName].time}`);
  47.   }
  48.  
  49.      let arrOfAllBands = Object.keys(AllBands);
  50.      let firstBand = arrOfAllBands[0];
  51.      console.log(firstBand);
  52.      
  53.      let entriesArrOfAllbands = Object.entries(AllBands);
  54.  
  55.      let BandFirstPlace = entriesArrOfAllbands[0];
  56.    for ( let element of BandFirstPlace[1].bandMembers){
  57.       console.log(`=> ${element}`);
  58.    }
  59. }
  60.  
  61. concert([
  62.   "Play; The Beatles; 2584",
  63.   "Add; The Beatles; John Lennon, George Harrison, Ringo Starr",
  64.   "Add; The Beatles; Paul McCartney, George Harrison",
  65.   "Add; The Rolling Stones; Brian Jones, Mick Jagger, Keith Richards",
  66.   "Play; The Rolling Stones; 4239",
  67.   "Start!",
  68. ]);
  69.  
  70. console.log(`------------`);
  71.  
  72. concert([
  73.   "Add; The Beatles; John Lennon, Paul McCartney",
  74.   "Add; The Beatles; Paul McCartney, George Harrison",
  75.   "Add; The Beatles; George Harrison, Ringo Starr",
  76.   "Play; The Beatles; 3698",
  77.   "Play; The Beatles; 3828",
  78.   "Start!",
  79. ]);
  80.  
  81. console.log(`------------`);
  82.  
  83. concert([
  84.   "Add; The Beatles; John Lennon, Paul McCartney, George Harrison, Ringo Starr",
  85.   "Play; The Beatles; 4569",
  86.   "Play; The Beatles; 2456",
  87.   "Play; Queen; 1250",
  88.   "Add; Queen; Freddie Mercury, Brian May, Roger Taylor, John Deacon",
  89.   "Play; Queen; 6215",
  90.   "Start!",
  91. ]);
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement