exercise 4.6 completed

This commit is contained in:
Pablo Martin 2025-06-05 15:44:55 +02:00
parent 0309a2cc95
commit e51f318929
3 changed files with 75 additions and 0 deletions

View file

@ -42,10 +42,30 @@ const favoritePost = (posts) => {
return favoritePost;
};
const mostPosts = (posts) => {
if (!posts || posts.length === 0) {
return null;
}
const countMap = posts.reduce((acc, post) => {
acc[post.author] = (acc[post.author] || 0) + 1;
return acc;
}, {});
// Use Object.entries to find the top author
const [author, postsCount] = Object.entries(countMap).reduce(
(max, entry) => (entry[1] > max[1] ? entry : max),
['', 0]
);
return { author, posts: postsCount };
};
module.exports = {
reverse,
average,
listHelper,
totalLikes,
favoritePost,
mostPosts,
};