Skip to content

Commit

Permalink
Move tags to outer scope and rename to tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramirisu committed Jan 31, 2024
1 parent 2d6688a commit d9e6ab1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 50 deletions.
55 changes: 24 additions & 31 deletions src/data/question.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,18 @@ const QUESTIONS = [
id: "q0602",
weight: {
societal: WEIGHTS.p100,

tags: {
homophobia: -1,
}
},
tag: {
homophobia: -1,
}
},
{
id: "q0603",
weight: {
societal: WEIGHTS.p100,

tags: {
death_penalty_abolitionists: 1,
}
},
tag: {
death_penalty_abolitionists: 1,
}
},
{
Expand All @@ -242,10 +240,9 @@ const QUESTIONS = [
weight: {
societal: WEIGHTS.p100,
sovereignty: WEIGHTS.p050,

tags: {
feminism_buffet: -1,
}
},
tag: {
feminism_buffet: -1,
}
},
{
Expand Down Expand Up @@ -286,10 +283,9 @@ const QUESTIONS = [
id: "q0705",
weight: {
societal: -WEIGHTS.p100,

tags: {
male_chauvinism: 1,
}
},
tag: {
male_chauvinism: 1,
}
},
{
Expand Down Expand Up @@ -338,21 +334,19 @@ const QUESTIONS = [
id: "q1000",
weight: {
sovereignty: WEIGHTS.p100,

tags: {
roc_independence: 1,
}
},
tag: {
roc_independence: 1,
}
},
{
id: "q1001",
weight: {
sovereignty: WEIGHTS.p150,
diplomatic: -WEIGHTS.p100,

tags: {
tw_independence: 1,
}
},
tag: {
tw_independence: 1,
}
},
{
Expand All @@ -379,10 +373,9 @@ const QUESTIONS = [
weight: {
sovereignty: -WEIGHTS.p150,
us_vs_china: -WEIGHTS.p100,

tags: {
prc_unification: 1,
}
},
tag: {
prc_unification: 1,
}
},
{
Expand All @@ -395,9 +388,9 @@ const QUESTIONS = [
{
id: "q1103",
weight: {
tags: {
roc_unification: 1,
}
},
tag: {
roc_unification: 1,
}
},
{
Expand Down
38 changes: 19 additions & 19 deletions src/pages/quiz/Quiz.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ import getScreenSize from '../../utils/getScreenSize'
import QUESTIONS from '../../data/question'

export const calculateScores = (questions, choices) => {
const getScore = (weights) => {
const getScoreWithMultiplier = (weights) => {
return weights.reduce((accu, value) => accu + value.weight * choices[value.id], 0.0)
const getScore = (props) => {
const getScoreWithMultiplier = (props) => {
return props.reduce((accu, value) => accu + value.prop * choices[value.id], 0.0)
}
const getAbsMaxScore = (weights) => {
return weights.reduce((accu, value) => accu + Math.abs(value.weight), 0.0)
const getAbsMaxScore = (props) => {
return props.reduce((accu, value) => accu + Math.abs(value.prop), 0.0)
}
const getPercentage = (bias, total) => 100 * (bias + total) / (2 * total)

const score = getScoreWithMultiplier(weights)
const maxScore = getAbsMaxScore(weights)
const score = getScoreWithMultiplier(props)
const maxScore = getAbsMaxScore(props)
return Math.round(getPercentage(score, maxScore))
}

const getTags = (weights) => {
return weights.reduce((accu, value) => {
const getTags = (props) => {
return props.reduce((accu, value) => {
if (choices[value.id] > 0.0) {
for (const [k, v] of Object.entries(value.tags || {})) {
for (const [k, v] of Object.entries(value.prop || {})) {
if (v > 0.0) {
accu.push(k)
}
}
}
if (choices[value.id] < 0.0) {
for (const [k, v] of Object.entries(value.tags || {})) {
for (const [k, v] of Object.entries(value.prop || {})) {
if (v < 0.0) {
accu.push(k)
}
Expand All @@ -44,14 +44,14 @@ export const calculateScores = (questions, choices) => {
}

return {
economic: getScore(questions.map((value) => ({ id: value.id, weight: value.weight.economic || 0.0 }))),
environmental: getScore(questions.map((value) => ({ id: value.id, weight: value.weight.environmental || 0.0 }))),
civil: getScore(questions.map((value) => ({ id: value.id, weight: value.weight.civil || 0.0 }))),
societal: getScore(questions.map((value) => ({ id: value.id, weight: value.weight.societal || 0.0 }))),
diplomatic: getScore(questions.map((value) => ({ id: value.id, weight: value.weight.diplomatic || 0.0 }))),
sovereignty: getScore(questions.map((value) => ({ id: value.id, weight: value.weight.sovereignty || 0.0 }))),
us_vs_china: getScore(questions.map((value) => ({ id: value.id, weight: value.weight.us_vs_china || 0.0 }))),
tags: getTags(questions.map((value) => ({ id: value.id, tags: value.weight.tags }))).join(',')
economic: getScore(questions.map((value) => ({ id: value.id, prop: value.weight.economic || 0.0 }))),
environmental: getScore(questions.map((value) => ({ id: value.id, prop: value.weight.environmental || 0.0 }))),
civil: getScore(questions.map((value) => ({ id: value.id, prop: value.weight.civil || 0.0 }))),
societal: getScore(questions.map((value) => ({ id: value.id, prop: value.weight.societal || 0.0 }))),
diplomatic: getScore(questions.map((value) => ({ id: value.id, prop: value.weight.diplomatic || 0.0 }))),
sovereignty: getScore(questions.map((value) => ({ id: value.id, prop: value.weight.sovereignty || 0.0 }))),
us_vs_china: getScore(questions.map((value) => ({ id: value.id, prop: value.weight.us_vs_china || 0.0 }))),
tags: getTags(questions.map((value) => ({ id: value.id, prop: value.tag }))).join(',')
}
}

Expand Down

0 comments on commit d9e6ab1

Please sign in to comment.