Home > Back-end >  sales data / JS
sales data / JS

Time:11-12

I have a sales data for a couple of years in an array: var= ['Jan-2019',325678], ['feb-2019', 456789], ['Mar-2019',-12890],.....['Dec-2021', 987460]

1 -want to calculate the net total amount of profit/losses over the entire period. 2- Average of the (changes) in profit/losses over the entire period - track the total change in profits from month to month and find the average(total/number of months) 3 - greatest increase in profit (month and amount)over the whole period 4 - greatest decrease3in profit (month and amount)over the whole period

Tried to solve number 1 using : `

const profitMonths = data.filter(el => el[1] > 0);
console.log("Total:", profitMonths.map(el => el[1]).reduce((a, b) => a   b));
console.log;

` but the sum I am getting is different from what excel and calculator is giving me. I will appreciate some help here

CodePudding user response:

Not sure what is the format of your original data records for each of the months. I assumed that your data format is like below. But you could get the sum of each months growth or loss (earnings) like this and also get what you were trying as well (profit months total sales):

const data = [
  ['Jan-2019', 325678],
  ['Feb-2019', 456789],
  ['Mar-2019', -12890],
];

const earningsArray = data.map((el) => el[1]);

const profitMonths = data.filter((el) => el[1] > 0);
const salesOnProfitMonths = profitMonths
  .map((el) => el[1])
  .reduce((accVal, curVal) => accVal   curVal, 0);

const avgOfProfitAndLoss =
  earningsArray.reduce((accVal, curVal) => accVal   curVal, 0) / data.length; // get the average of all total and losses

const maxMonth = {
  monthName: '',
  profit: 0,
};

const minMonth = {
  monthName: '',
  profit: 0,
};

data.forEach((month) => {
  if (month[1] > maxMonth.profit) {
    maxMonth.monthName = month[0];
    maxMonth.profit = month[1];
  }

  if (month[1] < minMonth.profit) {
    minMonth.monthName = month[0];
    minMonth.profit = month[1];
  }

  return { maxMonth, minMonth };
});

console.log('Total sale of profit months: ', salesOnProfitMonths);
console.log('Total average : ', avgOfProfitAndLoss);
console.log('The month with max profit is : ', maxMonth);
console.log('The month with min profit is : ', minMonth);

CodePudding user response:

Using .reduce() you can actually build an object to the returned based on all of the data from your original array.

const data = [['Jan-2019', 325678], ['feb-2019', 456789], ['Mar-2019',-12890], ['Dec-2021', 987460]]

let result = data.reduce((a, b, i) => {
    let d = (i > 1) ? a : {total: a[1], average: a[1], sumChange: 0, lastMonth: a[1], increase: a, decrease: a},
        change = b[1] - d.lastMonth
    
    d.total  = b[1]
    d.sumChange  = change
    d.lastMonth = b[1]
    d.average = d.sumChange / i
    d.increase = (d.increase[1] > change) ? d.increase : [b[0], change]
    d.decrease = (d.decrease[1] < change) ? d.decrease : [b[0], change]
    return d
})

console.log(result) // Return the full object
console.log(result.total) // Only return one value, the total

Based on the array/input you provided, this should provide a net total, average profit/loss, highest increase from the previous month, and highest decrease from the previous month.

EDIT

I had to make a few adjustments after getting some clarification. But this again should return a single object that holds values for everything requested by OP. (the sumChange and lastMonth values are only there to help with the .reduce() function month over month)

  • Related