I have a financial data for a few years which has:
'Month-year' , sales amount ex:
['jan-2020' , 984655],
['feb-2020', -21632],
and so on....
- for profit and - when loss is made. I want to calculate the following:
- numbers of months in the data
- amount of loss/profit over the entire period
- most profitable month (date and amount)
- worst month in terms of losses (date and amount) average change (total/number of months)
I tried the following at least to get numbers of months but was unsuccesful `
function getMonthDifference(startDate, endDate) {
return(
endDate.getMonth() -
startDate.getMonth()
);
}
console.log(getMonthDifference)
CodePudding user response:
You can achieve this with regular js functions. Read inline comments, check result by pressing blue button "Run code snippet":
// Data
const data = [
['jan-2020', 984655],
['feb-2020', -21632],
['mar-2020', 621632],
['apr-2020', 283726],
['may-2020', 10039],
['jun-2020', -40044],
['jul-2020', -10032],
['aug-2020', 50038],
['sep-2020', 915633],
['oct-2020', 494372],
['nov-2020', 374757],
['dec-2020', -51611]
];
// Get number of months in data
console.log("Total months in data:", data.length);
console.log("---------");
// Amount of losses over entire period
const lossMonths = data.filter(el => el[1] < 0);
console.log("All loss months:");
for(const mo of lossMonths) console.log(`${mo[0]}: ${mo[1]}`);
console.log("Sum of losses:", lossMonths.map(el => el[1]).reduce((a, b) => a b));
console.log("---------");
// Amount of profit over entire period
const profitMonths = data.filter(el => el[1] > 0);
console.log("All profit months:");
for(const mo of profitMonths) console.log(`${mo[0]}: ${mo[1]}`);
console.log("Sum of profit:", profitMonths.map(el => el[1]).reduce((a, b) => a b));
console.log("---------");
// Most worst month (date and amount)
const lossMonthSorted = lossMonths.sort((a, b) => a[1] > b[1]);
console.log("Most worst month:", lossMonthSorted[0].join(": "));
console.log("---------");
// Most profitable month (date and amount)
const profitMonthsSorted = profitMonths.sort((a, b) => a[1] < b[1]);
console.log("Most profitable month:", profitMonthsSorted[0].join(": "));
console.log("---------");
// Total and average
const total = data.map(el => el[1]).reduce((a, b) => a b);
const average = total / data.length;
console.log("Total:", total);
console.log("Average per month:", average.toFixed(2));
CodePudding user response:
Is this what you want to see? Did I misunderstand your question?
tempData = [['jan-2020',984655],['feb-2020',-21632]]
numbers = tempData.length
loss = tempData.filter(o => o[1] < 0 ).reduce( (o1,o2) => o1[1] o2[1]);
profit = tempData.filter(o => o[1] > 0 ).reduce( (o1,o2) => o1[1] o2[1]);
profitable = tempData.sort((o1, o2) => o1[1] > o2[1] ? -1 : (o1[1] < o2[1] ? 1 : 0))[0]
losses = tempData.sort((o1, o2) => o1[1] > o2[1] ? 1 : (o1[1] < o2[1] ? -1 : 0))[0]
avg = tempData.reduce( (o1,o2) => o1[1] o2[1])/numbers;
CodePudding user response:
many thanks for the reply. what is want to see is : Total months : x Total: $xxxx Average change :$-xxx Greatest Increase in Profits : Jan-2020 ($984655) Greatest decrease in profits : Feb-2022 ($-218332)