Skip to content

Commit 65aec0f

Browse files
committed
Refactor tally function to use Object.hasOwn for item counting and add test for 'toString' items
1 parent 8214876 commit 65aec0f

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

‎Sprint-2/implement/tally.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function tally(items) {
66
const counts = {};
77

88
for (const item of items) {
9-
if (counts[item] === undefined) {
9+
if (!Object.hasOwn(counts, item)) {
1010
counts[item] = 1;
1111
} else {
1212
counts[item] = counts[item] + 1;

‎Sprint-2/implement/tally.test.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ test("tally throws an error for invalid input", () => {
4444
expect(() => tally("hello")).toThrow();
4545
});
4646

47+
48+
test("tally counts items named toString", () => {
49+
expect(tally(["toString", "toString"])).toEqual({
50+
toString: 2,
51+
});
52+
});

0 commit comments

Comments
 (0)