Advertisement
Silviya7

4.Flight Schedule

Mar 28th, 2024
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function FlightSchedule(input){
  2.  
  3.     let FlightsReady=[];
  4.     let FlightsCAnceled=[];
  5.     let selectedFligts={};
  6.  
  7.     let Status='';
  8.  
  9.     for (let i = 0; i < input.length; i++) {
  10.        
  11.         switch(i){
  12.          case 0:
  13.          
  14.          for (const el1 of input[i]) {
  15.             let ArrFlights3=el1.split(' ');
  16.  
  17.             let number=ArrFlights3[0];
  18.             let Namedestination='';
  19.  
  20. for (let j = 1; j < ArrFlights3.length; j++) {
  21.    
  22.     Namedestination+=' '+ArrFlights3[j];
  23. }
  24.          
  25.             let objDestination= {number:number, namedestination:Namedestination.trim()};
  26.             FlightsReady.push(objDestination);
  27.            
  28.          }
  29.          
  30.          break;
  31.  
  32.          case 1:  
  33.          for (const el2 of input[i]) {
  34.             let[number,status]=el2.split(' ');
  35.          
  36.             let existObj=FlightsReady.find(f => f.number == number);
  37.  
  38.             if(existObj){        
  39.             let indexObject= FlightsReady.indexOf(existObj);
  40.             FlightsReady.splice(indexObject,1);
  41.             FlightsCAnceled.push(existObj);
  42.             }
  43.          }
  44.           break;
  45.  
  46.          case 2:
  47.          let element3='';
  48.          for (const el3 of input[i]) {
  49.              element3=el3.trim();
  50.  
  51.          }
  52.  
  53.          if (element3=='Ready to fly') {
  54.             Status='Ready to fly';
  55.             for (const flight of FlightsReady) {
  56.                 selectedFligts[flight.namedestination]=flight.number;
  57.                 //console.log(flight);
  58.             }
  59.          }
  60.          else if(element3 =='Cancelled'){
  61.             Status='Cancelled';
  62.             for (const flight of FlightsCAnceled) {
  63.                 selectedFligts[flight.namedestination]=flight.number;
  64.                 //console.log(flight);
  65.             }
  66.          }
  67.          break;
  68.         }
  69.                
  70.     }
  71.     let SortedFlights= '';
  72.     if( Status=='Cancelled'){
  73.         SortedFlights= Object.entries(selectedFligts).sort((a,b)=> a[0].localeCompare(b[0]));
  74.     }
  75.     else{
  76.  
  77.      SortedFlights= Object.entries(selectedFligts);
  78.     }
  79.      
  80.     for (const [key,value] of SortedFlights) {
  81.         console.log(`{ Destination: '${key}', Status: '${Status}' }`)
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement