Skip to content
Advertisement

Languages Statistic

I have to implement the “getLanguagesStatistic” function, which will help the IT magazine summarize 2019 in terms of the popularity of programming languages.

As input, the function receives an array of user reviews. You need to return an object in the format {languageName: count, anotherLanguageName: anotherCount, ...}, where languageName is the name of the language in the string, and count is the number of reviews left by programmers using this language.

In this case, only those user reviews that were left in 2019 should be taken into account. The revocation year is stored in the year field, the language in the language field.

Feedback is provided in the following format:

{ firstName: 'Noah', lastName: 'M.', country: 'Switzerland', continent: 'Europe', age: 19, language: 'C', year: 2019 }

Input data:

const data = [
  { firstName: 'Noah', lastName: 'M.', country: 'Switzerland', continent: 'Europe', age: 19, language: 'C', year: 2019 },
  { firstName: 'Anna', lastName: 'R.', country: 'Liechtenstein', continent: 'Europe', age: 52, language: 'JavaScript', year: 2019 },
  { firstName: 'Piter', lastName: 'G.', country: 'Sweden', continent: 'Europe', age: 30, language: 'JavaScript', year: 2019 },
  { firstName: 'Ramon', lastName: 'R.', country: 'Paraguay', continent: 'Americas', age: 29, language: 'Ruby', year: 2014 },
  { firstName: 'George', lastName: 'B.', country: 'England', continent: 'Europe', age: 81, language: 'C', year: 2016 },
];

const result = getLanguagesStatistic(data);

Output data:

console.log(result);
// { 
//   C: 1, 
//   JavaScript: 2 
// }

Function:

const getLanguagesStatistic = (feedbacks) => {
    //code here
};

I just managed to make the filter of the year. I tried the rest of the functionality through reduce, destructuring, but it doesn’t work, so I only write what I did.

Do I really need to use destructuring here?

My try:

const getLanguagesStatistic = (feedbacks) => {
      
    return feedbacks.filter( (f) => f.year == 2019)
    
};

Advertisement

Answer

Something like this

const getLanguagesStatistic = (feedbacks) => {
    return feedbacks.reduce((acc, {language, year}) => {
      if (year === 2019) {
        acc[language] = (acc[language]||0) + 1;
      }
      return acc;
    }, {});
};

const data = [
  { firstName: 'Noah', lastName: 'M.', country: 'Switzerland', continent: 'Europe', age: 19, language: 'C', year: 2019 },
  { firstName: 'Anna', lastName: 'R.', country: 'Liechtenstein', continent: 'Europe', age: 52, language: 'JavaScript', year: 2019 },
  { firstName: 'Piter', lastName: 'G.', country: 'Sweden', continent: 'Europe', age: 30, language: 'JavaScript', year: 2019 },
  { firstName: 'Ramon', lastName: 'R.', country: 'Paraguay', continent: 'Americas', age: 29, language: 'Ruby', year: 2014 },
  { firstName: 'George', lastName: 'B.', country: 'England', continent: 'Europe', age: 81, language: 'C', year: 2016 },
];

const result = getLanguagesStatistic(data);
console.log(result);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement