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();
Advertisement
Answer
This is what i came up with, i don’t know if it is according to your teacher’s (i assume) guidelines, but it should work. Maybe usable as a starting point. I added explanations.
The problem with your line vipPassengersAssignedToBusinessSeats = vipPassengers / businessSeatsPerFlight;
is that you might get back a float number. Picture having 3 vip passengers and 2 business seats – 3/2 returns a float. I would use loops for the issue.
Disclaimer: Not tested, and I have not used %
operator before, but I am sure you can adapt.
function Passengers() { function distributeAllSeatsToAllPassengers(num_flights, economy_seats, business_seats, passengers_vip, passengers_regular) { // number of flights is completely redundant in this example let passengers_vip_seated_business = 0, passengers_vip_seated_economy = 0, passengers_regular_seated_business = 0, passengers_regular_seated_economy = 0, passengers_vip_unseated = 0, // not necessary but nice to have passengers_economy_unseated = 0; // see above // use a for..of loop, assuming passengers_vip is an array for (let passenger of passengers_vip) { if (business_seats > 0) { business_seats--; // reduce number of available business seats passengers_vip_seated_business++; // increase number of seated passengers } else if (economy_seats > 0) { // if there are no business seats available, assign to economy_seats economy_seats--; passengers_vip_seated_economy++; } else { // no more seats, bummer! passengers_vip_unseated++; } } // do the same for regular passengers for (let passenger of passengers_regular) { if (business_seats > 0) { business_seats--; // reduce number of available business seats passengers_regular_seated_business++; // increase number of seated passengers } else if (economy_seats > 0) { // if there are no business seats available, assign to economy_seats economy_seats--; passengers_regular_seated_economy++; } else { // no more seats, bummer! passengers_economy_unseated++; } } return { passengers_vip_seated_business: passengers_vip_seated_business, passengers_vip_seated_economy: passengers_vip_seated_economy, passengers_regular_seated_business: passengers_regular_seated_business, passengers_regular_seated_economy: passengers_regular_seated_economy } } // returns the function, idk why but okay, maybe for functional programming return {distributeAllSeatsToAllPassengers} }