Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const INIT_AGE = 2020;
- const END_AGE = 2022;
- const calculateSoldsAmountBetweenAgesByParam = ({
- initAge,
- endAge,
- data,
- attribute,
- }) => {
- return data
- .filter(({ Date }) => {
- const [day, month, age] = Date.split('/');
- const isBetweenAgeRange =
- parseInt(age) >= initAge && parseInt(age) <= endAge;
- return isBetweenAgeRange;
- })
- .reduce((acc, current) => {
- return {
- ...acc,
- [current[attribute]]:
- acc[current[attribute]] !== undefined
- ? acc[current[attribute]] + 1
- : 1,
- };
- }, {});
- };
- const getBestSold = (records) => {
- let winner = null;
- let maxSold = 0;
- for (const [key, solds] of Object.entries(records)) {
- if (solds >= maxSold) {
- winner = { [key]: solds };
- maxSold = solds;
- }
- }
- return winner;
- };
- const getWorseSold = (records) => {
- let winner = null;
- let minSold = null;
- for (const [key, solds] of Object.entries(records)) {
- if (minSold === null) {
- winner = { [key]: solds };
- }
- if (solds <= minSold) {
- winner = { [key]: solds };
- minSold = solds;
- }
- }
- return winner;
- };
- const main = () => {
- const soldsAmountByZone = calculateSoldsAmountBetweenAgesByParam({
- initAge: INIT_AGE,
- endAge: END_AGE,
- attribute: 'Zone',
- data,
- });
- const soldsAmountByStore = calculateSoldsAmountBetweenAgesByParam({
- initAge: INIT_AGE,
- endAge: END_AGE,
- attribute: 'Store',
- data,
- });
- const soldsAmountBySalesManAt2021 = calculateSoldsAmountBetweenAgesByParam({
- initAge: 2021,
- endAge: 2021,
- attribute: 'Salesman name',
- data,
- });
- const soldAmount = Object.values(soldsAmountByZone).reduce(
- (acc, amount) => acc + amount,
- 0
- );
- const soldsPercentageByZone = Object.keys(soldsAmountByZone).reduce(
- (res, zone) => ({
- ...res,
- [zone]: (soldsAmountByZone[zone] / soldAmount).toFixed(2),
- }),
- {}
- );
- const bestSalesMan2021 = getBestSold(soldsAmountBySalesManAt2021);
- const worseSalesMan2021 = getWorseSold(soldsAmountBySalesManAt2021);
- const storeThatShouldClose = getWorseSold(soldsAmountByStore);
- return {
- soldsPercentageByZone,
- soldsAmountByStore,
- bestSalesMan2021,
- worseSalesMan2021,
- storeThatShouldClose,
- };
- };
- console.log(main());
Advertisement
Add Comment
Please, Sign In to add comment