I'm very new to JavaScript and I have been working through the Eloquent JavaScript (2nd) exercises in order to learn more. One particular exercise (5.3) has been giving me a lot of trouble.
The goal of the exercise is to take an array of objects that contains the date of death for a variety of people and group them by century based on death date. This is what I have so far:
function groupBy(array, groupOf) {
var groups = {};
array.forEach(function(element) {
var groupName = groupOf(element);
if (groupName in groups)
groups[groupName].push(element);
else
groups[groupName] = element;
});
return groups;
}
var byCentury = groupBy(ancestry, function(person) {
return Math.ceil(person.died / 100);
});
I believe the problem can be narrowed down to this line: groups[groupName] = element; but I don't understand why this is wrong.
Thanks for the help. I apologize in advance if this is obvious.