completed 4.5
This commit is contained in:
parent
160402f147
commit
0309a2cc95
3 changed files with 60 additions and 1 deletions
|
|
@ -27,9 +27,25 @@ const totalLikes = (posts) => {
|
||||||
return likeCount;
|
return likeCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
reverse,
|
reverse,
|
||||||
average,
|
average,
|
||||||
listHelper,
|
listHelper,
|
||||||
totalLikes,
|
totalLikes,
|
||||||
|
favoritePost,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
43
parts/4/blogApp/tests/favoritePost.test.js
Normal file
43
parts/4/blogApp/tests/favoritePost.test.js
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
const { test, describe } = require("node:test");
|
||||||
|
const assert = require("node:assert");
|
||||||
|
const { favoritePost } = require("../src/utils");
|
||||||
|
|
||||||
|
describe("favoritePost ", () => {
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const emptyArray = [];
|
||||||
|
|
||||||
|
const gibberish = "asdaSd123asd";
|
||||||
|
|
||||||
|
test("finds top properly", () => {
|
||||||
|
assert.strictEqual(favoritePost(posts), posts[0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("works fine with empty array", () => {
|
||||||
|
assert.strictEqual(favoritePost(emptyArray), null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("fails with gibberish input", () => {
|
||||||
|
const failedCall = () => {
|
||||||
|
favoritePost(gibberish);
|
||||||
|
};
|
||||||
|
assert.throws(failedCall, Error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -3,4 +3,4 @@ Exercises:
|
||||||
* [X] 4.2
|
* [X] 4.2
|
||||||
* [X] 4.3
|
* [X] 4.3
|
||||||
* [X] 4.4
|
* [X] 4.4
|
||||||
*
|
* [X] 4.5
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue