Skip to content

Commit f5c06bd

Browse files
committed
tally function fixed
1 parent 4dadf43 commit f5c06bd

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

Sprint-2/implement/tally.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
function tally(arr) {
2-
if (Array.isArray(arr)) {
2+
if (!Array.isArray(arr)) {
3+
throw new Error("The input should be an array!");
4+
}
5+
6+
const countObj = Object.create(null);
37

4-
let countObj = {};
5-
6-
for (const item of arr) {
7-
8-
if (countObj[item]) {
9-
countObj[item] = countObj[item] + 1;
10-
} else {
11-
countObj[item] = 1;
12-
}
8+
for (const item of arr) {
9+
10+
if (countObj[item]) {
11+
countObj[item] = countObj[item] + 1;
12+
} else {
13+
countObj[item] = 1;
1314
}
14-
15-
return countObj;
1615
}
17-
18-
else throw new Error("");
16+
17+
return countObj;
1918
}
2019

2120
module.exports = tally;

Sprint-2/implement/tally.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ test("tally on an array with duplicate items returns the counts for each uniqe i
4747
expect(givenInput).toEqual(targetOutput);
4848
})
4949

50+
test("tally on ", () => {
51+
const input = ["toString", "toString"];
52+
const givenInput = tally(input);
53+
const targetOutput = {toString: 2};
54+
55+
expect(givenInput).toEqual(targetOutput);
56+
})
57+
5058
// Given an invalid input like a string
5159
// When passed to tally
5260
// Then it should throw an error

0 commit comments

Comments
 (0)