mirror of
https://github.com/randyjc/Rahoot.git
synced 2026-03-14 04:25:35 +01:00
refactor(components): use arrow function syntax and improve consistency
This commit is contained in:
23
packages/web/src/utils/score.ts
Normal file
23
packages/web/src/utils/score.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
export const calculatePercentages = (
|
||||
objectResponses: Record<string, number>,
|
||||
): Record<string, string> => {
|
||||
const keys = Object.keys(objectResponses)
|
||||
const values = Object.values(objectResponses)
|
||||
|
||||
if (!values.length) {
|
||||
return {}
|
||||
}
|
||||
|
||||
const totalSum = values.reduce(
|
||||
(accumulator, currentValue) => accumulator + currentValue,
|
||||
0,
|
||||
)
|
||||
|
||||
const result: Record<string, string> = {}
|
||||
|
||||
keys.forEach((key) => {
|
||||
result[key] = `${((objectResponses[key] / totalSum) * 100).toFixed()}%`
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user