I solved an exercise from the book “Eloquent Javascript”. I want to share with you guys the solution. The exercise is from chapter 5. Summing up, I have to find the average age from the ancestors array per century. Here is my solution:
var ancestry = JSON.parse(require('../resources/ancestry')) //Top-down approach, functions are defined after their use //Necessary side-effect in function to print the output function printAverageAgePerCentury(ancestry){ var averageAgePerCentury = getAverageAgePerCentury(ancestry); Object.keys(averageAgePerCentury).forEach(function(century){ console.log(century.concat(": ").concat(averageAgePerCentury[century])) }) } function getAverageAgePerCentury(ancestry){ var agesPerCentury = getAgesPerCentury(ancestry); return agesPerCentury.reduce(function(averageAgePerCentury, agePerCentury){ if(averageAgePerCentury[agePerCentury.century]){ averageAgePerCentury[agePerCentury.century] = [average(averageAgePerCentury[agePerCentury.century].concat(agePerCentury.age))]; return averageAgePerCentury; } else { averageAgePerCentury[agePerCentury.century] = [agePerCentury.age]; return averageAgePerCentury; } }, {}) } function getAgesPerCentury(ancestry){ return ancestry.map(function(person){ return { century : whichCentury(person), age : person.died - person.born } }) } function average(array){ function plus(a,b){ return a + b} return array.reduce(plus) / array.length } function whichCentury(person){ return Math.ceil(person.died / 100) } //--------------------------------// printAverageAgePerCentury(ancestry);
- What do you think about the top-down approach? Do you prefer the opposite? Where functions are defined before their use.
- Is possible to make this solution more readable, more elegant?
- What could you do to make it better?