SHARE
TWEET

Untitled

a guest Jul 19th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.    function practiceSessions(input) {
  3.     let roads = {};
  4.     for (let line of input) {
  5.         if (line === 'END') {
  6.             break;
  7.         }
  8.         line = line.split('->');
  9.         let command = line[0];
  10.         let roadName = line[1];
  11.         let racer = line[2];
  12.  
  13.         if (command === 'Add') {
  14.             if (!roads.hasOwnProperty(roadName)) {
  15.                 roads[roadName] = [racer];
  16.             } else {
  17.                 roads[roadName].push(racer);
  18.             }
  19.         } else if (command === 'Move') {
  20.  
  21.             let nextRoad = line[3];
  22.             if (roads.hasOwnProperty(roadName)) {
  23.                 let indexOfRacer = roads[roadName].indexOf(racer);
  24.                 let racerName = roads[roadName][indexOfRacer];
  25.                 if (indexOfRacer !== -1) {
  26.                     roads[roadName].splice(indexOfRacer, 1);
  27.                     roads[nextRoad].push(racerName);
  28.                 }
  29.             }
  30.         } else if (command === 'Close') {
  31.             if (roads.hasOwnProperty(roadName)) {
  32.                 delete (roads[roadName]);
  33.             }
  34.         }
  35.     }
  36.     let sortedRoads = Object.entries(roads)
  37.         .sort((a, b) => b[1].length - a[1].length || a[0].localeCompare(b[0]));
  38.     console.log('Practice sessions:');
  39.     for (let [road, racers] of sortedRoads) {
  40.         console.log(road);
  41.         racers.forEach(racer => {
  42.             console.log(`++${racer}`)
  43.         });
  44.     }
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top