Todorov_Stanimir

02. Practice Sessions Fundament.Final Exam - 14 April 2019

Jun 6th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function practiceSessions(input) {
  2.  
  3.     let practiceSessios = [];
  4.     let road = '';
  5.     let racer = '';
  6.     let currentRoad = '';
  7.     let nextRoad = '';
  8.  
  9.     let currentOperation = input.shift().split('->');
  10.  
  11.     while (currentOperation[0] !== 'END') {
  12.  
  13.         if (currentOperation[0] === 'Add') {
  14.             road = currentOperation[1];
  15.             racer = currentOperation[2];
  16.             let RoadNotExist = true;
  17.             for (let i = 0; i < practiceSessios.length; i++) {
  18.                 if (practiceSessios[i].road === road) {
  19.                     practiceSessios[i].racer.push(racer);
  20.                     RoadNotExist = false;
  21.                     break;
  22.                 }
  23.             }
  24.             if (RoadNotExist) {
  25.                 practiceSessios.push({
  26.                     road: road,
  27.                     racer: [racer]
  28.                 });
  29.             }
  30.  
  31.         } else if (currentOperation[0] === 'Move') {
  32.             currentRoad = currentOperation[1];
  33.             racer = currentOperation[2];
  34.             nextRoad = currentOperation[3];
  35.             let racerExist = false;
  36.             for (let i = 0; i < practiceSessios.length; i++) {
  37.                 if (practiceSessios[i].road === currentRoad) {
  38.                     for (let y = 0; y < practiceSessios[i].racer.length; y++) {
  39.                         if (practiceSessios[i].racer[y] === racer) {
  40.                             practiceSessios[i].racer.splice(y, 1);
  41.                             racerExist = true;
  42.                             break;
  43.                         }
  44.                     }
  45.                 }
  46.                 if (racerExist === true) {
  47.                     break;
  48.                 }
  49.             }
  50.             if (racerExist === true) {
  51.                 for (let i = 0; i < practiceSessios.length; i++) {
  52.                     if (practiceSessios[i].road === nextRoad) {
  53.                         practiceSessios[i].racer.push(racer);
  54.  
  55.                     }
  56.                 }
  57.             }
  58.         } else if (currentOperation[0] === 'Close') {
  59.             closedRoad = currentOperation[1];
  60.             for (let i = 0; i < practiceSessios.length; i++) {
  61.                 if (practiceSessios[i].road === closedRoad) {
  62.                     practiceSessios.splice(i, 1);
  63.                 }
  64.  
  65.             }
  66.         }
  67.  
  68.         currentOperation = input.shift().split('->');
  69.     }
  70.     practiceSessios.sort(sortRoads);
  71.  
  72.     function sortRoads(a, b) {
  73.         if (a.racer.length < b.racer.length) {
  74.             return 1;
  75.         } else if (a.racer.length > b.racer.length) {
  76.             return -1;
  77.         } else if (a.racer.length === b.racer.length) {
  78.             return a.road.localeCompare(b.road);
  79.         }
  80.     }
  81.     console.log('Practice sessions:')
  82.     for (let i = 0; i < practiceSessios.length; i++) {
  83.         console.log(`${practiceSessios[i].road}`);
  84.         for (let y = 0; y < practiceSessios[i].racer.length; y++) {
  85.             console.log(`++${practiceSessios[i].racer[y]}`);
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment