Skip to content

Commit 2d7c3ad

Browse files
authored
Implement tally function with input validation
Added input validation and tallying functionality.
1 parent 78a88c6 commit 2d7c3ad

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(items) {
2+
if (!Array.isArray(items)) {
3+
throw new TypeError("Expected an array as input");
4+
}
5+
6+
return items.reduce((acc, item) => {
7+
acc[item] = (acc[item] || 0) + 1;
8+
return acc;
9+
}, {});
10+
}
211

312
module.exports = tally;

0 commit comments

Comments
 (0)