Skip to content

Commit 36fd665

Browse files
committed
tally test
1 parent e8e1de0 commit 36fd665

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

‎Sprint-2/implement/tally.js‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1-
function tally() {}
1+
function tally(array) {
2+
const charCount = {};
3+
4+
if (!Array.isArray(array)) {
5+
throw new Error();
6+
}
7+
8+
if (array.length === 0) {
9+
return charCount;
10+
}
11+
12+
for (const char of array) {
13+
if (charCount[char]) {
14+
charCount[char]++;
15+
} else {
16+
charCount[char] = 1;
17+
}
18+
}
19+
20+
return charCount;
21+
}
222

323
module.exports = tally;
24+
25+
/**
26+
* tally array
27+
*
28+
* In this task, you'll need to implement a function called tally
29+
* that will take a list of items and count the frequency of each item
30+
* in an array
31+
*
32+
* For example:
33+
*
34+
* tally(['a']), target output: { a: 1 }
35+
* tally(['a', 'a', 'a']), target output: { a: 3 }
36+
* tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 }
37+
*/

0 commit comments

Comments
 (0)