andrescj

DH

Nov 11th, 2022
1,382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const INIT_AGE = 2020;
  2. const END_AGE = 2022;
  3.  
  4. const calculateSoldsAmountBetweenAgesByParam = ({
  5.   initAge,
  6.   endAge,
  7.   data,
  8.   attribute,
  9. }) => {
  10.   return data
  11.     .filter(({ Date }) => {
  12.       const [day, month, age] = Date.split('/');
  13.  
  14.       const isBetweenAgeRange =
  15.         parseInt(age) >= initAge && parseInt(age) <= endAge;
  16.       return isBetweenAgeRange;
  17.     })
  18.     .reduce((acc, current) => {
  19.       return {
  20.         ...acc,
  21.         [current[attribute]]:
  22.           acc[current[attribute]] !== undefined
  23.             ? acc[current[attribute]] + 1
  24.             : 1,
  25.       };
  26.     }, {});
  27. };
  28.  
  29. const getBestSold = (records) => {
  30.   let winner = null;
  31.   let maxSold = 0;
  32.  
  33.   for (const [key, solds] of Object.entries(records)) {
  34.     if (solds >= maxSold) {
  35.       winner = { [key]: solds };
  36.       maxSold = solds;
  37.     }
  38.   }
  39.   return winner;
  40. };
  41.  
  42. const getWorseSold = (records) => {
  43.   let winner = null;
  44.   let minSold = null;
  45.  
  46.   for (const [key, solds] of Object.entries(records)) {
  47.     if (minSold === null) {
  48.       winner = { [key]: solds };
  49.     }
  50.     if (solds <= minSold) {
  51.       winner = { [key]: solds };
  52.       minSold = solds;
  53.     }
  54.   }
  55.   return winner;
  56. };
  57.  
  58. const main = () => {
  59.   const soldsAmountByZone = calculateSoldsAmountBetweenAgesByParam({
  60.     initAge: INIT_AGE,
  61.     endAge: END_AGE,
  62.     attribute: 'Zone',
  63.     data,
  64.   });
  65.  
  66.   const soldsAmountByStore = calculateSoldsAmountBetweenAgesByParam({
  67.     initAge: INIT_AGE,
  68.     endAge: END_AGE,
  69.     attribute: 'Store',
  70.     data,
  71.   });
  72.  
  73.   const soldsAmountBySalesManAt2021 = calculateSoldsAmountBetweenAgesByParam({
  74.     initAge: 2021,
  75.     endAge: 2021,
  76.     attribute: 'Salesman name',
  77.     data,
  78.   });
  79.  
  80.   const soldAmount = Object.values(soldsAmountByZone).reduce(
  81.     (acc, amount) => acc + amount,
  82.     0
  83.   );
  84.  
  85.   const soldsPercentageByZone = Object.keys(soldsAmountByZone).reduce(
  86.     (res, zone) => ({
  87.       ...res,
  88.       [zone]: (soldsAmountByZone[zone] / soldAmount).toFixed(2),
  89.     }),
  90.     {}
  91.   );
  92.  
  93.   const bestSalesMan2021 = getBestSold(soldsAmountBySalesManAt2021);
  94.   const worseSalesMan2021 = getWorseSold(soldsAmountBySalesManAt2021);
  95.   const storeThatShouldClose = getWorseSold(soldsAmountByStore);
  96.  
  97.   return {
  98.     soldsPercentageByZone,
  99.     soldsAmountByStore,
  100.     bestSalesMan2021,
  101.     worseSalesMan2021,
  102.     storeThatShouldClose,
  103.   };
  104. };
  105.  
  106. console.log(main());
  107.  
Advertisement
Add Comment
Please, Sign In to add comment