Skip to content

Commit fb8ea4b

Browse files
committed
fix(tally): prevent prototype collisions using null object
1 parent 03e4fa7 commit fb8ea4b

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

Sprint-2/implement/tally.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@ function tally(arrayOfItems) {
1111
throw new Error("Input must be an array");
1212
}
1313

14-
const countObject = {};
14+
const countObject = Object.create(null);
1515

1616
for (const item of arrayOfItems) {
17-
// If the item is already counted, add 1; otherwise start at 1
18-
const itemAlreadyCounted = countObject[item] !== undefined;
19-
20-
if (itemAlreadyCounted) {
21-
countObject[item] = countObject[item] + 1;
22-
} else {
23-
countObject[item] = 1;
24-
}
17+
const currentCount = countObject[item] || 0;
18+
countObject[item] = currentCount + 1;
2519
}
2620

2721
return countObject;

0 commit comments

Comments
 (0)