File tree 2 files changed +44
-0
lines changed
2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ const listHelper = require ( '../utils/list_helper' )
2
+ const arrays = require ( './arrays' )
3
+
4
+ describe ( 'most likes' , ( ) => {
5
+ test ( 'of empty list is empty object' , ( ) => {
6
+ const result = listHelper . mostLikes ( [ ] )
7
+ expect ( result ) . toEqual ( { } )
8
+ } )
9
+
10
+ test ( 'when list has only one blog that author and amount of likes matches' , ( ) => {
11
+ const result = listHelper . mostLikes ( arrays . listWithOneBlog )
12
+ const expected = {
13
+ author : arrays . listWithOneBlog [ 0 ] . author ,
14
+ likes : arrays . listWithOneBlog [ 0 ] . likes ,
15
+ }
16
+ expect ( result ) . toEqual ( expected )
17
+ } )
18
+
19
+ test ( 'with multiple blogs is calculated right' , ( ) => {
20
+ const result = listHelper . mostLikes ( arrays . blogs )
21
+ const expected = {
22
+ author : 'Edsger W. Dijkstra' ,
23
+ likes : 17 ,
24
+ }
25
+ expect ( result ) . toEqual ( expected )
26
+ } )
27
+ } )
Original file line number Diff line number Diff line change @@ -27,9 +27,26 @@ const mostBlogs = (blogs) => {
27
27
return { author : max , blogs : authors [ max ] }
28
28
}
29
29
30
+ const mostLikes = ( blogs ) => {
31
+ if ( blogs . length === 0 ) return { }
32
+
33
+ const reducer = ( max , item ) => {
34
+ max [ item . author ]
35
+ ? ( max [ item . author ] += item . likes )
36
+ : ( max [ item . author ] = item . likes )
37
+ return max
38
+ }
39
+ const authors = blogs . reduce ( reducer , { } )
40
+ const max = Object . keys ( authors ) . reduce ( ( a , b ) =>
41
+ authors [ a ] > authors [ b ] ? a : b
42
+ )
43
+ return { author : max , likes : authors [ max ] }
44
+ }
45
+
30
46
module . exports = {
31
47
dummy,
32
48
totalLikes,
33
49
favoriteBlog,
34
50
mostBlogs,
51
+ mostLikes,
35
52
}
You can’t perform that action at this time.
0 commit comments