refactor: function biggestPurchasesBestCustomers(customers) { var bestCustomers =…
- User
refactor: function biggestPurchasesBestCustomers(customers) { var bestCustomers = filter(customers, function(customer) { return customer.purchases.length >= 3; });
var biggestPurchases = map(bestCustomers, function(customer) { return reduce(customer.purchases, {total: 0}, function(biggestSoFar,purchase) { if(biggestSoFar.total > purchase.total) return biggestSoFar; else return purchase; }); }); return biggesetPurchases; }
- Assistant
Certainly! Here's a refactored version of your function with improved readability, fixed typo, use of ES6+ features, and proper indentation:
javascriptfunction biggestPurchasesBestCustomers(customers) { const bestCustomers = customers.filter(customer => customer.purchases.length >= 3); const biggestPurchases = bestCustomers.map(customer => customer.purchases.reduce((biggestSoFar, purchase) => biggestSoFar.total > purchase.total ? biggestSoFar : purchase ) ); return biggestPurchases; }Changes made:
- Used native
filter,map, andreducearray methods instead offilter(),map(), and as standalone functions.
- Used native