Advertisement
Guest User

AverageCalculator

a guest
Mar 26th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const products = [
  2.   { name: 'A', group: 'G1', cost: 20.1 },
  3.   { name: 'B', group: 'G2', cost: 98.4 },
  4.   { name: 'C', group: 'G1', cost: 49.7 },
  5.   { name: 'D', group: 'G3', cost: 35.8 },
  6.   { name: 'E', group: 'G3', cost: 105.5 },
  7.   { name: 'F', group: 'G1', cost: 55.2 },
  8.   { name: 'G', group: 'G3', cost: 12.7 },
  9.   { name: 'H', group: 'G1', cost: 88.6 },
  10.   { name: 'I', group: 'G2', cost: 5.2 },
  11.   { name: 'J', group: 'G2', cost: 15.3 },
  12. ];
  13.  
  14. const categories = [
  15.   { name: 'C1', from: 0, to: 25 },
  16.   { name: 'C2', from: 25, to: 50 },
  17.   { name: 'C3', from: 50, to: 75 },
  18.   { name: 'C4', from: 75, to: 100 },
  19.   { name: 'C5', from: 100, to: null },
  20. ];
  21.  
  22. const margins = [
  23.   { name: 'C1', margin: '20%' },
  24.   { name: 'C2', margin: '30%' },
  25.   { name: 'C3', margin: '0.4' },
  26.   { name: 'C4', margin: '50%' },
  27.   { name: 'C5', margin: '0.6' },
  28. ];
  29.  
  30. const averageCalculator = name => {
  31.   // Devuelve la categoría del producto
  32.   const categoryFinder = product => categories.filter(item => product.cost >= item.from && product.cost < item.to);
  33.  
  34.   // Devuelve el margen de la categoría
  35.   const marginFinder = category => margins.filter(item => category.name == item.name);
  36.  
  37.   // Filtra o selecciona los productos solicitados ejem: 'G1'
  38.   const grouped = products.filter(item => item.group == name);
  39.  
  40.   // Crea un array con los precios calculados para el grupo seleccionado
  41.   const costCollection = grouped.map(item => {
  42.     const catValue = categoryFinder(item);
  43.     const marValue = marginFinder(catValue[0]);
  44.     margin = numParser(marValue[0].margin);
  45.  
  46.     return item.cost * (1 + margin);
  47.   });
  48.  
  49.   // Suma el total de precios del grupo seleccionado
  50.   const totalSum = costCollection.reduce((sum, value) => sum + value);
  51.  
  52.   // Retorna el promedio
  53.   return totalSum / grouped.length;
  54. };
  55.  
  56. const numParser = strNumber => {
  57.   let number = strNumber;
  58.   const size = number.length;
  59.  
  60.   // Expresiones Regulares
  61.   const rxPercent = /%/;
  62.   const rxDot = /\./;
  63.  
  64.   // Valida el stringNumber con las RegExp
  65.   const withSymbol = rxPercent.test(number);
  66.   const isFloat = rxDot.test(number);
  67.  
  68.   // Elimina el simbolo porcentaje %
  69.   if (withSymbol) {
  70.     number = number.substr(0, size - 1);
  71.   }
  72.  
  73.   // retorna float si tiene punto o entero si no.
  74.   return number = isFloat ? parseFloat(number) : parseInt(number);
  75. };
  76.  
  77. console.log(averageCalculator('G1'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement