schat2-clive/assets/mash-average.js

29 lines
681 B
JavaScript
Raw Permalink Normal View History

2022-09-19 14:17:23 +00:00
'use strict';
const fs = require('fs').promises;
async function init() {
const rawDictionary = await fs.readFile('./mash-words.json', 'utf8');
const dictionary = JSON.parse(rawDictionary);
const averages = Object.entries(dictionary)
.reduce((accAverages, [length, anagramsMap]) => {
const anagramsList = Object.values(anagramsMap);
const total = anagramsList.reduce((acc, words) => acc + words.length, 0);
const average = total / anagramsList.length;
return {
...accAverages,
[length]: average,
};
}, {});
console.log(averages);
// CSV
console.log(Object.keys(averages).join(','));
console.log(Object.values(averages).join(','));
}
init();