Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
    
};

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading