Home > Back-end >  I need your helps for solving this question
I need your helps for solving this question

Time:10-01

A large number of people will go for a picnic. The number of buses will depend on the number of people. Suppose we have buses and micros. Each bus has a capacity of 36 people and each microbus has a capacity of 8 people and each public bus ticket costs 30 dollars. Now you have to write a function that will take a number (how many passengers) as a parameter. It will return you the total amount of public bus fares.

let publicBusFare = function publicBusFare(howManyPepole) {
  let microbus = 0;
  let bus = 0;
  let publicbusticket = 0;
  let restPepole = 0;
  let totalRestPepole = 0;
  let rp = 0;
    if (howManyPepole > 50) {
        restPepole = howManyPepole - 50;
        bus = bus   1;
        console.log(`first condition ${restPepole}`);
        console.log(bus);
        if (restPepole > 50) {
          totalRestPepole = restPepole - 50;
          bus = bus   1;
          console.log(`second condition ${totalRestPepole}`);
          console.log(bus);
          if (totalRestPepole > 50) {
            newRestPepole = totalRestPepole - 50;
            bus = bus   1;
            console.log(`second condition ${newRestPepole}`);
            console.log(bus);
            if (newRestPepole > 50) {
              rp = newRestPepole - 50;
              bus = bus   1;
              console.log(`second condition ${rp}`);
              console.log(bus);
            }
          }
        }
      }
  }
 
console.log(publicBusFare(277));

CodePudding user response:

You can divide the people every bus / microbus and using Math.floor() to get interger result,and then using modulus to get the rest people.

function publicBusFare(howManyPeople) {
    let microbus = 0;
    let bus = 0;
    let people = howManyPeople;
    bus = Math.floor(people / 36)
    people = people % 36
    console.log('People =' ,bus * 36 , "Bus = ",bus)
    console.log('Bus Cost = $' ,bus * 30)
    microbus = Math.floor(people / 8)
    people = people % 8
    console.log('People =' ,microbus * 8 , "Microbus = ",microbus)
    console.log('Rest People= ',people)
    return bus;
}

 console.log(publicBusFare(277));

CodePudding user response:

So with the little info you gave and your code, it looks like minibuses are free?

This code will do what you've asked and return the number of buses and minibuses needed. I have made the assumption that you will still need an extra minibus even when there is 1 passenger to go on it.

const capacity = {
  bus: 36,
  minibus: 8
}

const howMuch = passengers => {
  const busesRequired = Math.floor(passengers / capacity.bus);
  const remainingPassengers = passengers % capacity.bus;

  const miniBusesRequired = Math.ceil(remainingPassengers / capacity.minibus);

  const busCost = busesRequired * 30;
  //const minibusCost = miniBusesRequired * ?;

  return {
    busesRequired,
    miniBusesRequired,
    busCost,
    //minibusCost
  }
}

So howMuch(36) will return a cost of 30, because only 1 bus is needed and howMuch(52) will return 30 because there will be 1 bus and 2 minibuses (which I am assuming are free). If I am wrong, you can update and uncomment the bit to calculate the minibus cost.

CodePudding user response:

You can use a series if/else if/else flow control statements or chained ternary operators. Here are the conditions that could be used in determining the number of buses needed (which of course bus fare will be derived from).

Variable/Symbol Definition
passengers @param {number}
bus {number} 30
micro {number} 8
? Ternary operator placed after the condition ex. x > y ?... is equivalent to: if (x > y) {...
: Ternary operator placed where else if or else would be in a flow control statement ex. x > y ? 1 : 0 is equivalent to: if (x > y) { return 1 } else { return 0 }
&& AND operator wherein both conditions must be true in order to return true ex. x > 0 && x < 7 ? 1 : 0 is equivalent to: *"if x is greater than 0 AND x is less than 7 then return 1 else return 0. In other words if x is within the range of 1 to 6 return 1, otherwise return 0.
Condition Description Result
passengers < bus && passengers > micro ? [1, 0] : if number of passengers is less than 36 AND number of passengers is greater than 8, return [1, 0] else if [1, 0] Bus: 1, Micro-bus: 0
passengers <= micro ? [0, 1] : number of passengers is less than or equal to 8 return [0, 1] else [0, 1] Bus: 0, Micro-bus: 1
[ (passengers / bus) (passengers % bus > micro ? 1 : 0),... return [... the sum of (passengers divided by 36) and 1 (if the remainder of passengers divided by 36 is greater than 8) else 0,... [ passengers/36 (1 if there is more than 8 left over) or 0,... Bus: 1 or more
...(passengers % bus <= micro) && (passengers % bus > 0) ? 1 : 0 ]; if the remainder of passengers divided by 36 is less than or equal to 8 AND the remainder of passengers divided by 36 is greater than zero return 1 else 0 ...if the remainder of passengers divided by 36 is between 1 to 8 return 1 else 0 Micro-bus: 1 or 0

So basically:

  • if there's 9 to 36 passengers, 1 bus is needed
  • if there's 1 to 8 passengers, 1 micro-bus is needed
  • if there's 37 or more passengers,
    • divide the number of passengers by 36...
    • if there's a remainder...
      • ...and the remainder is greater than 8, add another bus
      • but if the remainder is between 1 to 8, add a micro-bus
  • so only a single micro-bus is needed if there's less than 9 passengers total or as a remainder

function busFleet(passengers) {
  const bus = 36;
  const micro = 8;
  
  const fleet = passengers < bus && passengers > micro ? [1, 0] : 
              passengers <= micro ? [0, 1] : 
              [
                (passengers / bus)   (passengers % bus > micro ? 1 : 0), 
                (passengers % bus <= micro) && (passengers % bus > 0) ? 1 : 0
              ];
  
  const fares = (parseInt(fleet[0])   parseInt(fleet[1])) * 30;
  
  return `Passengers: ${passengers}
Bus: ${parseInt(fleet[0])}
Micro-bus: ${parseInt(fleet[1])}
Fares: $${fares}`;
}

console.log(busFleet(108));
console.log(busFleet(45));
console.log(busFleet(16));
console.log(busFleet(79));

  • Related