Liliana797979

Passengers per flight

Nov 16th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function flight(input) {
  2.     let numberOfAirCompanies = Number(input[0]);
  3.     let mostPassengersPerFlight = 0;
  4.     let companyWinner = "";
  5.     let index = 1;
  6.  
  7.     for (let i = 0; i < numberOfAirCompanies; i++) {
  8.         let flightCounter = 0;
  9.         let tempPassengers = 0;
  10.         let averagePersonPerFlight = 0;
  11.         let tempCompany = input[index];
  12.         index++;
  13.         let command = input[index];
  14.  
  15.         while (true) {
  16.             if (command === "Finish") {
  17.                 break;
  18.             } else {
  19.                 flightCounter++;
  20.                 tempPassengers += Number(input[index]);
  21.                 index++;
  22.                 command = input[index];
  23.             }
  24.         }
  25.         index++;
  26.         averagePersonPerFlight = Math.floor(tempPassengers / flightCounter);
  27.         console.log(`${tempCompany}: ${averagePersonPerFlight} passengers.`);
  28.  
  29.         if (averagePersonPerFlight < mostPassengersPerFlight) {
  30.             companyWinner = tempCompany;
  31.             mostPassengersPerFlight = averagePersonPerFlight;
  32.         }
  33.     }
  34.     console.log(`${companyWinner} has most passengers per flight: ${mostPassengersPerFlight}`);
  35. }
  36.  
  37. flight(3,
  38.   "WizzAir"
  39.     180,
  40.     230,
  41.     100,
  42.     "Finish",
  43.     "BulgariaAir"
  44.     50,
  45.     60,
  46.     90,
  47.     "Finish",
  48.     "Lufthansa",
  49.     260,
  50.     320,
  51.     "Finish");
Advertisement
Add Comment
Please, Sign In to add comment