Created
April 3, 2020 01:22
-
-
Save tekul/cf43cadbf03f00f1a3c8c8d1c1a1ce58 to your computer and use it in GitHub Desktop.
Identify top users/purchases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function idBestUsers() { | |
let monthlyOrders = [...arguments]; | |
let totalMonths = monthlyOrders.length; | |
let clientOrders = new Object(); | |
monthlyOrders[0].forEach(clientId => { | |
if(!clientOrders[clientId]) { | |
clientOrders[clientId] = new Array(monthlyOrders.length); | |
clientOrders[clientId][0] = 0; | |
} | |
clientOrders[clientId][0]++; | |
}); | |
var month; | |
for (month = 1; month < totalMonths; month++) { | |
monthlyOrders[month].forEach(clientId => { | |
if(clientOrders[clientId]) { | |
if(!clientOrders[clientId][month]) { | |
clientOrders[clientId][month] = 0; | |
} | |
clientOrders[clientId][month]++; | |
} | |
}); | |
} | |
for (var clientId in clientOrders) { | |
var totalOrdersForClient = 0; | |
var everyMonth = true; | |
var orders = clientOrders[clientId]; | |
for(var month = 0; month < totalMonths; month++) { | |
if(!orders[month]) { | |
everyMonth = false; | |
break; | |
} | |
totalOrdersForClient += orders[month]; | |
} | |
if (everyMonth) { | |
clientOrders[clientId] = totalOrdersForClient; | |
} else { | |
delete clientOrders[clientId]; | |
} | |
} | |
var clientsByOrderTotal = new Object(); | |
Object.keys(clientOrders).forEach(clientId => { | |
var clientTotal = clientOrders[clientId]; | |
if(!clientsByOrderTotal[clientTotal]) { | |
clientsByOrderTotal[clientTotal] = [clientId]; | |
} else { | |
clientsByOrderTotal[clientTotal].push(clientId); | |
} | |
}); | |
var clientsByOrderTotalArray = Object.keys(clientsByOrderTotal).map(total => [parseInt(total), clientsByOrderTotal[total]]); | |
clientsByOrderTotalArray.forEach(a => a[1].sort()); | |
return clientsByOrderTotalArray.sort((a, b) => b[0] - a[0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment