Skip to content

Commit 058fc4e

Browse files
authored
Implement tally function with input validation
Add input validation and counting logic to tally function.
1 parent 094c32b commit 058fc4e

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

‎Sprint-2/implement/tally.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1-
function tally() {}
1+
function tally(items) {
2+
if (!Array.isArray(items)) {
3+
throw new Error("Input must be an array");
4+
}
5+
6+
const counts = {};
7+
8+
for (const item of items) {
9+
if (counts[item]) {
10+
counts[item]++;
11+
} else {
12+
counts[item] = 1;
13+
}
14+
}
15+
16+
return counts;
17+
}
218

319
module.exports = tally;

0 commit comments

Comments
 (0)