completed exercise 4.7

This commit is contained in:
Pablo Martin 2025-06-05 15:48:19 +02:00
parent e51f318929
commit b839aeba78
3 changed files with 74 additions and 1 deletions

View file

@ -52,7 +52,6 @@ const mostPosts = (posts) => {
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]
@ -61,6 +60,24 @@ const mostPosts = (posts) => {
return { author, posts: postsCount };
};
const mostLikes = (posts) => {
if (!posts || posts.length === 0) {
return null;
}
const likesMap = posts.reduce((acc, post) => {
acc[post.author] = (acc[post.author] || 0) + post.likes;
return acc;
}, {});
const [author, likesCount] = Object.entries(likesMap).reduce(
(max, entry) => (entry[1] > max[1] ? entry : max),
['', 0]
);
return { author, likes: likesCount };
};
module.exports = {
reverse,
average,
@ -68,4 +85,5 @@ module.exports = {
totalLikes,
favoritePost,
mostPosts,
mostLikes
};