exercise 4.6 completed
This commit is contained in:
parent
0309a2cc95
commit
e51f318929
3 changed files with 75 additions and 0 deletions
|
|
@ -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,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue