Created
July 16, 2024 20:47
-
-
Save kevinfiol/e1f56b04855760c75f7b549ee96c49e6 to your computer and use it in GitHub Desktop.
karat samples
This file contains hidden or 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
const counts = [ "900,google.com", | |
"60,mail.yahoo.com", | |
"10,mobile.sports.yahoo.com", | |
"40,sports.yahoo.com", | |
"300,yahoo.com", | |
"10,stackoverflow.com", | |
"20,overflow.com", | |
"5,com.com", | |
"2,en.wikipedia.org", | |
"1,m.wikipedia.org", | |
"1,mobile.sports", | |
"1,google.co.uk"]; | |
function calculateClicksByDomain(counts = []) { | |
const map = new Map(); | |
for (const count of counts) { | |
let [clicks, url] = count.split(','); | |
clicks = Number(clicks); | |
const parts = url.split('.'); | |
for (let part = '', i = parts.length - 1; i >= 0; i--) { | |
part = parts[i] + (part ? '.' + part : ''); | |
if (!map.has(part)) map.set(part, clicks); | |
else map.set(part, map.get(part) + clicks); | |
} | |
} | |
return map; | |
} | |
const map = calculateClicksByDomain(counts); | |
console.log(map); |
Author
kevinfiol
commented
Jul 16, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment