I just finished a kata and would love to get some constructive feedback regarding code quality, readability, organization.
Objectives
- fetch members of polish parliament (poslowie)
- group them by occupation
- sort occupations by number of members and occupation name
- get top 5 entries
- print result on the screen
Sample result
Code
(function() {
'use strict';
const POSLOWIE_ENDPOINT = 'https://api-v3.mojepanstwo.pl/dane/poslowie/';
fetch(POSLOWIE_ENDPOINT)
.then(response => response.json())
.then(data => data.Dataobject)
.then(sumByOccupation)
.then(occupationsMap => {
return occupationsMap
.sort(compareByCountAndName)
.slice(0, 5);
})
.then(printToScreen)
.catch(e => console.error(e));
function sumByOccupation(poslowie) {
return Array.from(
poslowie
.map(posel => posel.data['poslowie.zawod'])
.reduce((map, occupation) => {
const numberOfEntries = map.get(occupation) || 0;
return map.set(occupation, numberOfEntries + 1);
}, new Map())
).map(([occupation, count]) => {
return {occupation, count};
});
}
function compareByCountAndName(a, b) {
return b.count - a.count || (a.occupation).localeCompare(b.occupation);
}
function printToScreen(data) {
console.table(data);
}
})();