2025-06-04 17:58:33 +02:00
|
|
|
const reverse = (string) => {
|
|
|
|
|
return string.split("").reverse().join("");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const average = (array) => {
|
|
|
|
|
const reducer = (sum, item) => {
|
|
|
|
|
return sum + item;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return array.length === 0 ? 0 : array.reduce(reducer, 0) / array.length;
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-04 23:35:31 +02:00
|
|
|
const listHelper = (posts) => {
|
|
|
|
|
console.log("lol");
|
|
|
|
|
return 1;
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-04 23:47:43 +02:00
|
|
|
const totalLikes = (posts) => {
|
|
|
|
|
if (!posts) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const likeCount = posts
|
|
|
|
|
.map((post) => post.likes)
|
|
|
|
|
.reduce((cum, value) => cum + value, 0);
|
|
|
|
|
|
|
|
|
|
return likeCount;
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-04 23:59:57 +02:00
|
|
|
const favoritePost = (posts) => {
|
|
|
|
|
if (!posts || posts.length === 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const highestLikes = posts.reduce(
|
|
|
|
|
(max, post) => (post.likes > max ? (max = post.likes) : max),
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const favoritePost = posts.find((post) => post.likes == highestLikes);
|
|
|
|
|
|
|
|
|
|
return favoritePost;
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-05 15:44:55 +02:00
|
|
|
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;
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
const [author, postsCount] = Object.entries(countMap).reduce(
|
|
|
|
|
(max, entry) => (entry[1] > max[1] ? entry : max),
|
|
|
|
|
['', 0]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return { author, posts: postsCount };
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-05 15:48:19 +02:00
|
|
|
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 };
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-04 17:58:33 +02:00
|
|
|
module.exports = {
|
|
|
|
|
reverse,
|
|
|
|
|
average,
|
2025-06-04 23:35:31 +02:00
|
|
|
listHelper,
|
2025-06-04 23:47:43 +02:00
|
|
|
totalLikes,
|
2025-06-04 23:59:57 +02:00
|
|
|
favoritePost,
|
2025-06-05 15:44:55 +02:00
|
|
|
mostPosts,
|
2025-06-05 15:48:19 +02:00
|
|
|
mostLikes
|
2025-06-04 17:58:33 +02:00
|
|
|
};
|