completed exercise 4.7
This commit is contained in:
parent
e51f318929
commit
b839aeba78
3 changed files with 74 additions and 1 deletions
|
|
@ -52,7 +52,6 @@ const mostPosts = (posts) => {
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
// Use Object.entries to find the top author
|
|
||||||
const [author, postsCount] = Object.entries(countMap).reduce(
|
const [author, postsCount] = Object.entries(countMap).reduce(
|
||||||
(max, entry) => (entry[1] > max[1] ? entry : max),
|
(max, entry) => (entry[1] > max[1] ? entry : max),
|
||||||
['', 0]
|
['', 0]
|
||||||
|
|
@ -61,6 +60,24 @@ const mostPosts = (posts) => {
|
||||||
return { author, posts: postsCount };
|
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 = {
|
module.exports = {
|
||||||
reverse,
|
reverse,
|
||||||
average,
|
average,
|
||||||
|
|
@ -68,4 +85,5 @@ module.exports = {
|
||||||
totalLikes,
|
totalLikes,
|
||||||
favoritePost,
|
favoritePost,
|
||||||
mostPosts,
|
mostPosts,
|
||||||
|
mostLikes
|
||||||
};
|
};
|
||||||
|
|
|
||||||
54
parts/4/blogApp/tests/mostLikes.test.js
Normal file
54
parts/4/blogApp/tests/mostLikes.test.js
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
const { test, describe } = require("node:test");
|
||||||
|
const assert = require("node:assert");
|
||||||
|
const { mostLikes } = require("../src/utils");
|
||||||
|
|
||||||
|
describe("most likes ", () => {
|
||||||
|
const posts = [
|
||||||
|
{
|
||||||
|
_id: "5a422aa71b54a676234d17f8",
|
||||||
|
title: "Go To Statement Considered Harmful",
|
||||||
|
author: "Edsger W. Dijkstra",
|
||||||
|
url: "https://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf",
|
||||||
|
likes: 5,
|
||||||
|
__v: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
_id: "5a422aa71b54a676234d17f8",
|
||||||
|
title: "Go To Statement Considered Harmful",
|
||||||
|
author: "Edsger W. Dijkstra",
|
||||||
|
url: "https://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf",
|
||||||
|
likes: 2,
|
||||||
|
__v: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
_id: "123",
|
||||||
|
title: "Lololo",
|
||||||
|
author: "John Doe",
|
||||||
|
url: "https://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf",
|
||||||
|
likes: 1,
|
||||||
|
__v: 0,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const emptyArray = [];
|
||||||
|
|
||||||
|
const gibberish = "asdaSd123asd";
|
||||||
|
|
||||||
|
test("finds top author properly", () => {
|
||||||
|
assert.deepStrictEqual(mostLikes(posts), {
|
||||||
|
author: "Edsger W. Dijkstra",
|
||||||
|
likes: 7,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("works fine with empty array", () => {
|
||||||
|
assert.strictEqual(mostLikes(emptyArray), null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("fails with gibberish input", () => {
|
||||||
|
const failedCall = () => {
|
||||||
|
mostLikes(gibberish);
|
||||||
|
};
|
||||||
|
assert.throws(failedCall, Error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -5,3 +5,4 @@ Exercises:
|
||||||
* [X] 4.4
|
* [X] 4.4
|
||||||
* [X] 4.5
|
* [X] 4.5
|
||||||
* [X] 4.6
|
* [X] 4.6
|
||||||
|
* [X] 4.7
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue