Skip to content

Commit 4a6fdfd

Browse files
committed
feat: 통계에 평균값 추가
1 parent 8ff9a65 commit 4a6fdfd

File tree

3 files changed

+71
-26
lines changed

3 files changed

+71
-26
lines changed

src/app/stats/candidate/page.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default function StatsByCandidate() {
3232
return (
3333
<div className={styles.statsContainer}>
3434
<section>
35-
<h3>가장 질문이 많은 후보</h3>
35+
<h3>가장 많은 질문을 받은 후보</h3>
3636
{loading && <p>Loading...</p>}
3737
{hasData && <p>{candidateWithMaxRequest.name}({candidateWithMaxRequest.party}) : {candidateWithMaxRequest.requests}</p>}
3838
</section>

src/app/stats/city/page.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ export default function StatsByCity() {
2020
}))
2121

2222
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}))
2527
return candidatesRequests
2628
}
2729

@@ -31,7 +33,7 @@ export default function StatsByCity() {
3133
const cities = await response.json()
3234
const citiesRequests = await Promise.all(cities.map(async ({name}) => {
3335
const requests = await fetchCandidatesRequestsByCity(name)
34-
return { city: name, requests }
36+
return { city: name, ...requests}
3537
}))
3638
setRequestsByCities(citiesRequests)
3739
setLoading(false)
@@ -42,34 +44,55 @@ export default function StatsByCity() {
4244
}, [])
4345

4446
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+
}
5164

5265
return (
5366
<div className={styles.statsContainer}>
5467
<section>
5568
<h3>지역별 질문 수</h3>
5669
{loading && <p>Loading...</p>}
57-
{hasData && requestsByCities.map(({city, requests}) => (
70+
{hasData && requestsByCities.map(({city, requests, candidates}) => (
5871
<div key={city}>
59-
<p>{city} : {requests}</p>
72+
<p>{city} : {requests} / {candidates} =&gt; 후보 한 명당 평균 {getAverage(requests, candidates)}</p>
6073
</div>
6174
))}
6275
</section>
6376
<section>
64-
<h3>가장 질문이 많은 지역</h3>
77+
<h3>가장 많은 질문을 받은 지역</h3>
6578
{loading && <p>Loading...</p>}
6679
{hasData && <p>{cityWithMaxRequest.city} : {cityWithMaxRequest.requests}</p>}
6780
</section>
6881
<section>
69-
<h3>가장 질문이 적은 지역</h3>
82+
<h3>가장 적은 질문을 받은 지역</h3>
7083
{loading && <p>Loading...</p>}
7184
{hasData && <p>{cityWithMinRequest.city} : {cityWithMinRequest.requests}</p>}
7285
</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>
7396
</div>
7497
)
7598
}

src/app/stats/party/page.js

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export default function StatsByParty() {
1616
return data.requests
1717
}))
1818
const sum = requests.reduce((acc, counter) => acc + counter, 0)
19-
return sum
19+
const length = candidatesByParties.length
20+
return {requests: sum, candidates: length}
2021
}
2122

2223
const getRequestsByParties = async () => {
@@ -25,7 +26,7 @@ export default function StatsByParty() {
2526
const parties = await response.json()
2627
const partiesRequests = await Promise.all(parties.map(async ({name}) => {
2728
const requests = await fetchCandidatesRequestsByParty(name)
28-
return { party: name, requests }
29+
return { party: name, ...requests }
2930
}))
3031
setRequestsByParties(partiesRequests)
3132
setLoading(false)
@@ -36,34 +37,55 @@ export default function StatsByParty() {
3637
}, [])
3738

3839
const hasData = !loading && requestsByParties.length > 0
39-
const partyWithMaxRequest = requestsByParties.reduce((acc, {party, requests}) => {
40-
return requests > acc.requests ? { party, requests } : acc
41-
}, { party: '', requests: 0 })
42-
const partyWithMinRequest = requestsByParties.reduce((acc, {party, requests}) => {
43-
return requests < acc.requests ?{ party, requests } : acc
44-
}, { party: '', requests: Number.MAX_SAFE_INTEGER })
40+
const partyWithMaxRequest = requestsByParties.reduce((acc, cur) => {
41+
return cur.requests > acc.requests ? cur : acc
42+
}, { party: '', requests: Number.MIN_SAFE_INTEGER, candidates: 0 })
43+
const partyWithMinRequest = requestsByParties.reduce((acc, cur) => {
44+
return cur.requests < acc.requests ? cur : acc
45+
}, { party: '', requests: Number.MAX_SAFE_INTEGER, candidates: 0 })
46+
47+
const partyWithMaxAverage = requestsByParties.reduce((acc, cur) => {
48+
return (cur.requests / cur.candidates) > (acc.requests / acc.candidates) ? cur : acc
49+
}, { party: '', requests: Number.MIN_SAFE_INTEGER, candidates: 0 })
50+
const partyWithMinAverage = requestsByParties.reduce((acc, cur) => {
51+
return (cur.requests / cur.candidates) < (acc.requests / acc.candidates) ? cur : acc
52+
}, { party: '', requests: Number.MAX_SAFE_INTEGER, candidates: 0 })
53+
54+
function getAverage(requests, candidates) {
55+
return (requests / candidates).toFixed()
56+
}
4557

4658
return (
4759
<div className={styles.statsContainer}>
4860
<section>
4961
<h3>정당별 질문 수</h3>
5062
{loading && <p>Loading...</p>}
51-
{hasData && requestsByParties.map(({party, requests}) => (
63+
{hasData && requestsByParties.map(({party, requests, candidates}) => (
5264
<div key={party}>
53-
<p>{party} : {requests}</p>
65+
<p>{party} : {requests} / {candidates} =&gt; 후보 한 명당 평균 {getAverage(requests, candidates)}</p>
5466
</div>
5567
))}
5668
</section>
5769
<section>
58-
<h3>가장 질문이 많은 정당</h3>
70+
<h3>가장 많은 질문을 받은 정당</h3>
5971
{loading && <p>Loading...</p>}
6072
{hasData && <p>{partyWithMaxRequest.party} : {partyWithMaxRequest.requests}</p>}
6173
</section>
6274
<section>
63-
<h3>가장 질문이 적은 정당</h3>
75+
<h3>가장 적은 질문을 받은 정당</h3>
6476
{loading && <p>Loading...</p>}
6577
{hasData && <p>{partyWithMinRequest.party} : {partyWithMinRequest.requests}</p>}
6678
</section>
79+
<section>
80+
<h3>후보 한 명당 가장 많은 질문을 받은 정당</h3>
81+
{loading && <p>Loading...</p>}
82+
{hasData && <p>{partyWithMaxAverage.party} : {getAverage(partyWithMaxAverage.requests, partyWithMaxAverage.candidates)}</p>}
83+
</section>
84+
<section>
85+
<h3>후보 한 명당 가장 적은 질문을 받은 정당</h3>
86+
{loading && <p>Loading...</p>}
87+
{hasData && <p>{partyWithMinAverage.party} : {getAverage(partyWithMinAverage.requests, partyWithMinAverage.candidates)}</p>}
88+
</section>
6789
</div>
6890
)
6991
}

0 commit comments

Comments
 (0)