Skip to content

Commit 6d8eefd

Browse files
committed
write complete function to taly
1 parent 78041d6 commit 6d8eefd

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

‎Sprint-2/implement/tally.js‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1-
function tally() {}
1+
function tally(arr) {
2+
let countOfItemsObj = {};
3+
if (!Array.isArray(arr)) {
4+
throw new Error("Invalid input");
5+
} else if (arr.length < 1) {
6+
return countOfItemsObj;
7+
} else {
8+
for (let element = 0; element < arr.length; element++) {
9+
const exist = Object.hasOwn(countOfItemsObj, arr[element]);
10+
if (!exist) {
11+
countOfItemsObj[arr[element]] = 1;
12+
} else {
13+
countOfItemsObj[arr[element]] = countOfItemsObj[arr[element]] + 1;
14+
}
15+
}
16+
}
17+
return countOfItemsObj;
18+
}
219

320
module.exports = tally;

0 commit comments

Comments
 (0)