Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

kata output

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);
     }
})();
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.