I've got a project I'm working on that deals with managing flights and passenger numbers. I'm currently stuck implementing the function below, any suggestions for how I could go about this would be excellent, what I currently have is below which I don't think is correct, thanks again.
Question:
In the Passengers() function, write a distributeAllSeatsToAllPassengers() function that receives as parameters the number of VIP passengers, the number of regular passengers, the number of flights, the number of business seats per flight, and the number of economy seats per flight. It returns an object containing the following fields: VIP passengers with business seats; VIP passengers with economy seats; regular passengers with business seats; regular passengers with economy seats.
The distribution rules need to be followed in this order:
First, assign VIP passengers to business seats, until either the VIP passengers or the business seats are consumed.
Then, if there are still VIP passengers, assign them to economy seats, until either the VIP passengers or the economy seats are consumed.
Then, if there are still business seats, assign regular passengers to business seats, until either the regular passengers or the business seats are consumed.
Then, if there are still economy seats, assign regular passengers to economy seats, until either the regular passengers or the economy seats are consumed. You have to assign as many passengers as possible. There may be either passengers or seats that are left.
After the definition of the distributeAllSeatsToAllPassengers() function, a line of code will export the functions from the module: return {distributeAllSeatsToAllPassengers};
My current solution:
import { supportsEsModules } from "mocha/lib/utils";
function Passengers() {
function distributeAllSeatsToAllPassengers(vipPassengers,
regularPassengers, nrOfFlights, businessSeatsPerFlight, economySeatsPerFlight)
{
let vipPassengersAssignedToBusinessSeats = 0;
let vipPassengersAssignedToEconomySeats = 0;
let regularPassengersAssignedToBusinessSeats = 0;
vipPassengersAssignedToBusinessSeats = vipPassengers / businessSeatsPerFlight;
vipPassengersNotSeated = vipPassengers % businessSeatsPerFlight;
if (vipPassengersNotSeated !== 0)
{
vipPassengersAssignedToEconomySeats = vipPassengersNotSeated / economySeatsPerFlight;
vipPassengersStillNotSeated = vipPassengersNotSeated % economySeatsPerFlight;
}
if (businessSeatsPerFlight !== 0)
{
regularPassengersAssignedToBusinessSeats = regularPassengers / businessSeatsPerFlight;
regularPassengersNotSeated = regularPassengers % businessSeatsPerFlight;
}
if (economySeatsPerFlight !== 0)
{
regualarPassengersAssignedToEconomySeats = regularPassengersNotSeated / economySeatsPerFlight;
regularPassengersStillNotSeated = regularPassengersNotSeated % economySeatsPerFlight;
}
var distributedPassengers = {
vipPassengersWithBusinessSeats: vipPassengersAssignedToBusinessSeats,
vipPassengersWithEconomySeats: vipPassengersAssignedToEconomySeats,
regularPassengersWithBusinessSeats: regularPassengersAssignedToBusinessSeats,
regularPassengersWithEconomySeats: regularPassengersAssignedToEconomySeats
};
return { distributedPassengers };
}
return { distributeAllSeatsToAllPassengers };
}
module.exports = Passengers();