Skip to content

Commit c698639

Browse files
Alex JamshidiAlex Jamshidi
authored andcommitted
updated tally for edge case toString
1 parent 8427bfd commit c698639

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

Sprint-2/implement/tally.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ function tally(array) {
22
if (!Array.isArray(array)) {
33
throw new Error("Invalid array");
44
}
5-
tally = {};
5+
const count = Object.create(null);
66
array.forEach((item) => {
7-
if (item in tally) {
8-
tally[item] += 1;
9-
} else tally[item] = 1;
7+
if (item in count) {
8+
count[item] += 1;
9+
} else count[item] = 1;
1010
});
11-
return tally;
11+
return count;
1212
}
1313

1414
module.exports = tally;

Sprint-2/implement/tally.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ describe("tally", () => {
3333
// When passed to tally
3434
// Then it should return counts for each unique item
3535
it("tally returns counts for each unique item", () => {
36-
const array = ["a", "a", "b", "c"];
37-
expect(tally(array)).toEqual({ a: 2, b: 1, c: 1 });
36+
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
37+
expect(tally(["toString", "toString"])).toEqual({ toString: 2 });
3838
});
3939

4040
// Given an invalid input like a string

0 commit comments

Comments
 (0)