Open alamenai opened 1 year ago
make an object, so you can create the key then the values
Question : Find the 10 most spoken languages:
const mostSpokenLanguage = (countries, limit) => countries .map((country) => country.languages) .flat() .reduce((acc, language) => { return acc[language] ? ++acc[language] : (acc[language] = 1), acc; }, []); console.log(mostSpokenLanguage(countriesList, 10));
Current :
Uzbek: 2, Turkmen: 2, Swedish: 3, Albanian: 3, Arabic: 25, English: 91,
Expected:
{country: Uzbek, count:2} {.....}, {.....}
const mostSpokenLanguage = (countries, limit) =>
countries
.map((country) => country.languages)
.flat()
.reduce((acc, language) => {
let counter = acc[language] ? ++acc[language] : (acc[language] = 1)
return {country: language, count: counter}, acc;
}, []);
console.log(mostSpokenLanguage(countriesList, 10));
Above code will work ig
Question : Find the 10 most spoken languages:
Current :
Expected: