简体   繁体   中英

How to calculate values in node.js

I'm working on an application like Food Panda. I'm m facing an issue. I'm calculating driver payouts (eg if a driver earns 4.3 dollars per order) I'm trying to get the sum for every month but my code assigning same value to these months when driver was out of service.

Please tell me what I'm doing wrong my code:

parameters: toDate=2020-10-21&fromDate=2021-11-21

const resturantOrders = await DelivigoOrder.findAll({
  attributes: [
    "Id",
    "OrderNumber",
    "OrderStatusId",
    "TotalPrice",
    "OrderAcceptTime",
    "DriverId",
    "ResturentId"
  ],
  where: {
    DriverId: driverId,
    IsActive: true,
    // OrderAcceptTime: { [Op.between]: [fromDate, toDate] },
    $or: [
      { OrderStatusId: 50 },
      { OrderStatusId: 55 },
      { OrderStatusId: 60 },
      { OrderStatusId: 70 },
      { OrderStatusId: 80 },
    ],
  },
});

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct ", "Nov ", "Dec"];

var orderPayouts = [];
let payout = 0;
for (let index = 0; index < months.length; index++) {
  const element = months[index];

  for (let index = 0; index < resturantOrders.length; index++) {
    const order = resturantOrders[index];

    if (element === InternalServices.getMonthName(order.OrderAcceptTime)) {
      console.log(element, index, "in");
      payout = payout + 4.3;
    }
  }

  orderPayouts.push({
    month: element,
    payout: payout
  });
};

Output

{
  "result": [{
    "month": "Jan",
    "payout": 55.899999999999984
  }, {
    "month": "Feb",
    "payout": 55.899999999999984
  }, {
    "month": "Mar",
    "payout": 55.899999999999984
  }, {
    "month": "Apr",
    "payout": 55.899999999999984
  }, {
    "month": "May",
    "payout": 55.899999999999984
  }, {
    "month": "Jun",
    "payout": 55.899999999999984
  }, {
    "month": "Jul",
    "payout": 55.899999999999984
  }, {
    "month": "Aug",
    "payout": 55.899999999999984
  }, {
    "month": "Sep",
    "payout": 55.899999999999984
  }, {
    "month": "Oct",
    "payout": 150.5
  }, {
    "month": "Nov",
    "payout": 150.5
  }, {
    "month": "Dec",
    "payout": 150.5
  }]
}

driver works in just two months Jan and Oct but my code assing the Jan value to all other months same with OCT.

You declare ley payout outside of the loop. You should consider declaring it INSIDE the loop

...
for (let index = 0; index < months.length; index++) {
  let payout = 0;
  const element = months[index];
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM