Guest User

Untitled

a guest
Jun 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2. * Given an array of orders, find the top 3 customers with the
  3. * highest lifetime value (those that spent the most over time).
  4. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  5.  
  6.  
  7. // * * * * * * * * * * * * * * * * * * * * * * * * * * *
  8. // Start with the initial implementation.
  9. //
  10. // One approach to solving this task would be to create
  11. // a map of the customerId and a sum of their order totals.
  12. // Convert this map to an array so we can sort the orders
  13. // from highest to lowest. Display the top three customers.
  14. // * * * * * * * * * * * * * * * * * * * * * * * * * * *
  15. const orders = [
  16. {orderId: 1, customerId: 1, total: 10.50},
  17. {orderId: 2, customerId: 1, total: 11.50},
  18. {orderId: 3, customerId: 2, total: 12.50},
  19. {orderId: 4, customerId: 3, total: 130.50},
  20. {orderId: 5, customerId: 7, total: 24.50},
  21. {orderId: 6, customerId: 2, total: 64.50},
  22. {orderId: 7, customerId: 4, total: 40.50},
  23. {orderId: 8, customerId: 4, total: 29.50},
  24. {orderId: 9, customerId: 6, total: 61.50},
  25. {orderId: 10, customerId: 7, total: 110.00}
  26. ];
  27.  
  28. //Build a temporary table to track total orders for a customer.
  29. let orderTotalsMap = {};
  30. for (let i = 0; i < orders.length; i++) {
  31. let order = orders[i];
  32.  
  33. if (orderTotalsMap[order.customerId] === undefined) {
  34. orderTotalsMap[order.customerId] = order.total;
  35. } else {
  36. orderTotalsMap[order.customerId] += order.total;
  37. }
  38. }
  39.  
  40. //Convert the map to an array and sort by the aggregated order total.
  41. const keysArr = Object.keys(orderTotalsMap);
  42. let orderTotalsArr = [];
  43.  
  44. for (let i = 0; i < keysArr.length; i++) {
  45. const totals = {
  46. customerId: keysArr[i],
  47. total: orderTotalsMap[keysArr[i]]
  48. }
  49. orderTotalsArr.push(totals);
  50. }
  51.  
  52. orderTotalsArr.sort(function (a, b) {
  53. return b.total - a.total;
  54. });
  55.  
  56. const topThreeCustomers = orderTotalsArr.slice(0, 3);
  57.  
  58. console.log('The top three customers:', topThreeCustomers);
  59.  
  60.  
  61.  
  62. // * * * * * * * * * * * * * * * * * * * * * * * * * * *
  63. // It's difficult to understand what's going on in the
  64. // example above. With some refactoring, this can be much
  65. // more legible.
  66. //
  67. // To fulfill our requirements, we must:
  68. // 1. Get the lifetime value for each customer.
  69. // 2. Sort by the total amount and return the top 3 customers.
  70. // * * * * * * * * * * * * * * * * * * * * * * * * * * *
  71.  
  72. const orders = [
  73. {orderId: 1, customerId: 1, total: 10.50},
  74. {orderId: 2, customerId: 1, total: 11.50},
  75. {orderId: 3, customerId: 2, total: 12.50},
  76. {orderId: 4, customerId: 3, total: 130.50},
  77. {orderId: 5, customerId: 7, total: 24.50},
  78. {orderId: 6, customerId: 2, total: 64.50},
  79. {orderId: 7, customerId: 4, total: 40.50},
  80. {orderId: 8, customerId: 4, total: 29.50},
  81. {orderId: 9, customerId: 6, total: 61.50},
  82. {orderId: 10, customerId: 7, total: 110.00}
  83. ];
  84.  
  85. //Get order totals for each customer.
  86. const getCustomerLifetimeValues = (acc, order) => {
  87. const {customerId, total} = order;
  88.  
  89. if (acc[customerId] === undefined) {
  90. acc[customerId] = {customerId, total}
  91. } else {
  92. acc[customerId].total += total;
  93. }
  94.  
  95. return acc;
  96. }
  97.  
  98. //Comparator for sorting by total.
  99. const comparator = (a, b) => b.total - a.total;
  100.  
  101. //Now, for the code that matters! Let's worry less about "how this works" and more about the "why we need it".
  102. let lifetimeValuesObj = orders.reduce(getCustomerLifetimeValues, {});
  103. let topThreeCustomers = Object.values(lifetimeValuesObj).sort(comparator).slice(0, 3);
  104.  
  105. console.log('The top three customers:', topThreeCustomers);
  106.  
  107. // For this solution, we are able to make reasable assumptions about the code because it's much easier to understand.
  108. // By looking at lines 102 - 103, we know that the code will calculate the totals for each customer and provide top
  109. // three customers by order totals. We didn't have to study the implementation to determine what is going on. Sweet!
Add Comment
Please, Sign In to add comment