@@ -20,8 +20,10 @@ export default function StatsByCity() {
20
20
} ) )
21
21
22
22
const sum = requests . reduce ( ( acc , counter ) => acc + counter , 0 )
23
- return await acc + sum
24
- } , 0 )
23
+ const length = candidatesByRegions . length
24
+ const { requests : accRequests , candidates : accCandidates } = await acc
25
+ return await { requests : accRequests + sum , candidates : accCandidates + length }
26
+ } , ( { requests : 0 , candidates : 0 } ) )
25
27
return candidatesRequests
26
28
}
27
29
@@ -31,7 +33,7 @@ export default function StatsByCity() {
31
33
const cities = await response . json ( )
32
34
const citiesRequests = await Promise . all ( cities . map ( async ( { name} ) => {
33
35
const requests = await fetchCandidatesRequestsByCity ( name )
34
- return { city : name , requests }
36
+ return { city : name , ... requests }
35
37
} ) )
36
38
setRequestsByCities ( citiesRequests )
37
39
setLoading ( false )
@@ -42,34 +44,55 @@ export default function StatsByCity() {
42
44
} , [ ] )
43
45
44
46
const hasData = ! loading && requestsByCities . length > 0
45
- const cityWithMaxRequest = requestsByCities . reduce ( ( acc , { city, requests} ) => {
46
- return requests > acc . requests ? { city, requests } : acc
47
- } , { city : '' , requests : 0 } )
48
- const cityWithMinRequest = requestsByCities . reduce ( ( acc , { city, requests} ) => {
49
- return requests < acc . requests ?{ city, requests } : acc
50
- } , { city : '' , requests : Number . MAX_SAFE_INTEGER } )
47
+ const cityWithMaxRequest = requestsByCities . reduce ( ( acc , cur ) => {
48
+ return cur . requests > acc . requests ? cur : acc
49
+ } , { city : '' , requests : Number . MIN_SAFE_INTEGER , candidates : 0 } )
50
+ const cityWithMinRequest = requestsByCities . reduce ( ( acc , cur ) => {
51
+ return cur . requests < acc . requests ? cur : acc
52
+ } , { city : '' , requests : Number . MAX_SAFE_INTEGER , candidates : 0 } )
53
+
54
+ const cityWithMaxAverage = requestsByCities . reduce ( ( acc , cur ) => {
55
+ return ( cur . requests / cur . candidates ) > ( acc . requests / acc . candidates ) ? cur : acc
56
+ } , { city : '' , requests : Number . MIN_SAFE_INTEGER , candidates : 0 } )
57
+ const cityWithMinAverage = requestsByCities . reduce ( ( acc , cur ) => {
58
+ return ( cur . requests / cur . candidates ) < ( acc . requests / acc . candidates ) ? cur : acc
59
+ } , { city : '' , requests : Number . MAX_SAFE_INTEGER , candidates : 0 } )
60
+
61
+ function getAverage ( requests , candidates ) {
62
+ return ( requests / candidates ) . toFixed ( )
63
+ }
51
64
52
65
return (
53
66
< div className = { styles . statsContainer } >
54
67
< section >
55
68
< h3 > 지역별 질문 수</ h3 >
56
69
{ loading && < p > Loading...</ p > }
57
- { hasData && requestsByCities . map ( ( { city, requests} ) => (
70
+ { hasData && requestsByCities . map ( ( { city, requests, candidates } ) => (
58
71
< div key = { city } >
59
- < p > { city } : { requests } </ p >
72
+ < p > { city } : { requests } / { candidates } => 후보 한 명당 평균 { getAverage ( requests , candidates ) } 개 </ p >
60
73
</ div >
61
74
) ) }
62
75
</ section >
63
76
< section >
64
- < h3 > 가장 질문이 많은 지역</ h3 >
77
+ < h3 > 가장 많은 질문을 받은 지역</ h3 >
65
78
{ loading && < p > Loading...</ p > }
66
79
{ hasData && < p > { cityWithMaxRequest . city } : { cityWithMaxRequest . requests } </ p > }
67
80
</ section >
68
81
< section >
69
- < h3 > 가장 질문이 적은 지역</ h3 >
82
+ < h3 > 가장 적은 질문을 받은 지역</ h3 >
70
83
{ loading && < p > Loading...</ p > }
71
84
{ hasData && < p > { cityWithMinRequest . city } : { cityWithMinRequest . requests } </ p > }
72
85
</ section >
86
+ < section >
87
+ < h3 > 후보 한 명당 가장 많은 질문을 받은 지역</ h3 >
88
+ { loading && < p > Loading...</ p > }
89
+ { hasData && < p > { cityWithMaxAverage . city } : { getAverage ( cityWithMaxAverage . requests , cityWithMaxAverage . candidates ) } </ p > }
90
+ </ section >
91
+ < section >
92
+ < h3 > 후보 한 명당 가장 적은 질문을 받은 지역</ h3 >
93
+ { loading && < p > Loading...</ p > }
94
+ { hasData && < p > { cityWithMinAverage . city } : { getAverage ( cityWithMinAverage . requests , cityWithMinAverage . candidates ) } </ p > }
95
+ </ section >
73
96
</ div >
74
97
)
75
98
}
0 commit comments