11const tally = require ( "./tally.js" ) ;
22
3- /**
4- * tally array
5- *
6- * In this task, you'll need to implement a function called tally
7- * that will take a list of items and count the frequency of each item
8- * in an array
9- *
10- * For example:
11- *
12- * tally(['a']), target output: { a: 1 }
13- * tally(['a', 'a', 'a']), target output: { a: 3 }
14- * tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 }
15- */
16-
17- // Acceptance criteria:
18-
19- // Given a function called tally
20- // When passed an array of items
21- // Then it should return an object containing the count for each unique item
223test ( "given an array of items an object containing the counter for each item" , ( ) => {
234 expect ( tally ( [ "a" , "a" ] ) ) . toEqual ( { a : 2 } ) ;
245} ) ;
25- // Given an empty array
26- // When passed to tally
27- // Then it should return an empty object
6+
287test ( "tally on an empty array returns an empty object" , ( ) => {
298 expect ( tally ( [ ] ) ) . toEqual ( { } ) ;
309} ) ;
3110
32- // Given an array with duplicate items
33- // When passed to tally
34- // Then it should return counts for each unique item
3511test ( "given an array of items an object containing the counter for each item" , ( ) => {
3612 expect ( tally ( [ "a" , "a" , "c" , "b" , "b" , "d" ] ) ) . toEqual ( {
3713 a : 2 ,
@@ -40,19 +16,6 @@ test("given an array of items an object containing the counter for each item", (
4016 d : 1 ,
4117 } ) ;
4218} ) ;
43- // Given an invalid input like a string
44- // When passed to tally
45- // Then it should throw an error
46-
47- /*
48- try {
49- tally("Invalid");
50- console.error(
51- "Test failed: Expected an error to be thrown for invalid input"
52- );
53- } catch (error) {
54- console.log("Test passed: Error thrown for invalid input");
55- }*/
5619
5720test ( "tally on an empty array returns an empty object" , ( ) => {
5821 expect ( ( ) => tally ( "string" ) ) . toThrow ( "Invalid input!" ) ;
0 commit comments