Skip to content

Commit 493e3f1

Browse files
Implement tally function to count occurrences of elements in an array with input validation
1 parent 3f4a115 commit 493e3f1

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Sprint-2/implement/tally.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
function tally() {}
1+
function tally(arr) {
2+
const objectCount={}
3+
if(!Array.isArray(arr)){
4+
throw new Error("Invalid input");
5+
}
6+
if(arr.length===0){
7+
return {}
8+
}
9+
for(const element of arr){
10+
objectCount[element]=objectCount[element] ? objectCount[element] +1:1;
11+
}
12+
return objectCount
13+
14+
}
15+
216

317
module.exports = tally;

0 commit comments

Comments
 (0)