completed 4.4
This commit is contained in:
parent
a41907fe4b
commit
160402f147
3 changed files with 58 additions and 0 deletions
|
|
@ -15,8 +15,21 @@ const listHelper = (posts) => {
|
||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const totalLikes = (posts) => {
|
||||||
|
if (!posts) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const likeCount = posts
|
||||||
|
.map((post) => post.likes)
|
||||||
|
.reduce((cum, value) => cum + value, 0);
|
||||||
|
|
||||||
|
return likeCount;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
reverse,
|
reverse,
|
||||||
average,
|
average,
|
||||||
listHelper,
|
listHelper,
|
||||||
|
totalLikes,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
43
parts/4/blogApp/tests/totalLikes.test.js
Normal file
43
parts/4/blogApp/tests/totalLikes.test.js
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
const { test, describe } = require("node:test");
|
||||||
|
const assert = require("node:assert");
|
||||||
|
const { totalLikes } = require("../src/utils");
|
||||||
|
|
||||||
|
describe("total 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,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const emptyArray = [];
|
||||||
|
|
||||||
|
const gibberish = "asdaSd123asd";
|
||||||
|
|
||||||
|
test("counts likes properly", () => {
|
||||||
|
assert.strictEqual(totalLikes(posts), 7);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("works fine with empty array", () => {
|
||||||
|
assert.strictEqual(totalLikes(emptyArray), 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("fails with gibberish input", () => {
|
||||||
|
const failedCall = () => {
|
||||||
|
totalLikes(gibberish);
|
||||||
|
};
|
||||||
|
assert.throws(failedCall, Error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -2,3 +2,5 @@ Exercises:
|
||||||
* [X] 4.1
|
* [X] 4.1
|
||||||
* [X] 4.2
|
* [X] 4.2
|
||||||
* [X] 4.3
|
* [X] 4.3
|
||||||
|
* [X] 4.4
|
||||||
|
*
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue