Skip to content

Commit 1c17fbf

Browse files
committed
Implement tally function to count occurrences of items in an array and handle non-array inputs
1 parent 1874dec commit 1c17fbf

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎Sprint-2/implement/tally.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
function tally() {}
1+
function tally() {
2+
if (!Array.isArray(items)) {
3+
throw new TypeError("Input must be an array");
4+
}
5+
return items.reduce((acc, item) => {
6+
// Increment count if key exists, otherwise initialize to 1
7+
acc[item] = (acc[item] || 0) + 1;
8+
return acc;
9+
}, {});
10+
}
211

312
module.exports = tally;

0 commit comments

Comments
 (0)