File tree Expand file tree Collapse file tree 2 files changed +21
-14
lines changed
Expand file tree Collapse file tree 2 files changed +21
-14
lines changed Original file line number Diff line number Diff line change 11function tally ( arr ) {
2- if ( Array . isArray ( arr ) ) {
2+ if ( ! Array . isArray ( arr ) ) {
3+ throw new Error ( "The input should be an array!" ) ;
4+ }
5+
6+ const countObj = Object . create ( null ) ;
37
4- let countObj = { } ;
5-
6- for ( const item of arr ) {
7-
8- if ( countObj [ item ] ) {
9- countObj [ item ] = countObj [ item ] + 1 ;
10- } else {
11- countObj [ item ] = 1 ;
12- }
8+ for ( const item of arr ) {
9+
10+ if ( countObj [ item ] ) {
11+ countObj [ item ] = countObj [ item ] + 1 ;
12+ } else {
13+ countObj [ item ] = 1 ;
1314 }
14-
15- return countObj ;
1615 }
17-
18- else throw new Error ( "" ) ;
16+
17+ return countObj ;
1918}
2019
2120module . exports = tally ;
Original file line number Diff line number Diff line change @@ -47,6 +47,14 @@ test("tally on an array with duplicate items returns the counts for each uniqe i
4747 expect ( givenInput ) . toEqual ( targetOutput ) ;
4848} )
4949
50+ test ( "tally on " , ( ) => {
51+ const input = [ "toString" , "toString" ] ;
52+ const givenInput = tally ( input ) ;
53+ const targetOutput = { toString : 2 } ;
54+
55+ expect ( givenInput ) . toEqual ( targetOutput ) ;
56+ } )
57+
5058// Given an invalid input like a string
5159// When passed to tally
5260// Then it should throw an error
You can’t perform that action at this time.
0 commit comments