Skip to content

Commit 1a06c50

Browse files
committed
feat: enable explanation overrides
1 parent 31660ce commit 1a06c50

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/data/quizzes/questionBank.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { QuestionBank, QuestionBankConfig } from "@/lib/types"
1+
import type {
2+
ChoiceLetter,
3+
QuestionBank,
4+
QuestionBankConfig,
5+
} from "@/lib/types"
26

37
/**
48
* This file is used to generate the question bank for the quizzes.
@@ -30,6 +34,25 @@ import type { QuestionBank, QuestionBankConfig } from "@/lib/types"
3034
* - "what-is-ethereum-1-c-explanation"
3135
* - "what-is-ethereum-1-d-label"
3236
* - "what-is-ethereum-1-d-explanation"
37+
*
38+
* To re-use answer explanations from other choices within the same question,
39+
* append an optional explanationOverrides array to the question object.
40+
* This take the format of an array of 1-indexed answer choices to re-use, with
41+
* the position in the array representing which question to override. First
42+
* position is "a".
43+
*
44+
* Example - Re-use the first answer explanation for all choices:
45+
* "what-is-ethereum": [
46+
* { totalAnswers: 4, correctAnswer: 2, explanationOverrides: [1, 1, 1, 1] }
47+
* ]
48+
*
49+
* With that alteration, requires the following strings in learn-quizzes.json:
50+
* - "what-is-ethereum-1-prompt"
51+
* - "what-is-ethereum-1-a-label"
52+
* - "what-is-ethereum-1-a-explanation"
53+
* - "what-is-ethereum-1-b-label"
54+
* - "what-is-ethereum-1-c-label"
55+
* - "what-is-ethereum-1-d-label"
3356
*/
3457
const questionBankConfig: QuestionBankConfig = {
3558
"what-is-ethereum": [
@@ -137,19 +160,26 @@ const charFromIdx = (idx: number) => String.fromCharCode(97 + idx)
137160
const questionBank = Object.entries(questionBankConfig).reduce(
138161
(acc, [topicKey, value]) => {
139162
for (const [idx, question] of value.entries()) {
140-
const { totalAnswers, correctAnswer } = question
163+
const { totalAnswers, correctAnswer, explanationOverrides } = question
141164
const questionKey = `${topicKey}-${idx + 1}`
142165
const questionObject = {
143166
prompt: `${questionKey}-prompt`,
144167
answers: Array(totalAnswers)
145168
.fill(0)
146169
.map((_, i) => {
147-
const choice = charFromIdx(i) as "a" | "b" | "c" | "d"
170+
const choice = charFromIdx(i) as ChoiceLetter
171+
const exIdx =
172+
explanationOverrides && explanationOverrides[i]
173+
? explanationOverrides[i] - 1
174+
: i
175+
const exLetter = charFromIdx(exIdx) as ChoiceLetter
176+
148177
const id = `${questionKey}-${choice}`
178+
const exId = `${questionKey}-${exLetter}`
149179
return {
150180
id,
151181
label: `${id}-label`,
152-
explanation: `${id}-explanation`,
182+
explanation: `${exId}-explanation`,
153183
}
154184
}),
155185
correctAnswerId: `${questionKey}-${charFromIdx(correctAnswer - 1)}`,

src/lib/types.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,19 @@ export type LoadingState<T> =
161161
| { loading: false; data: T }
162162
| { loading: false; error: unknown }
163163

164-
/**
165-
* Quiz data types
166-
*/
167-
export type QuestionTemplate = {
168-
totalAnswers: 2 | 3 | 4
169-
correctAnswer: 1 | 2 | 3 | 4
164+
// Quiz data types
165+
166+
export type ChoiceLetter = "a" | "b" | "c" | "d"
167+
168+
type ChoiceNumber = 1 | 2 | 3 | 4
169+
type TotalAnswers = 2 | 3 | 4
170+
171+
type QuestionTemplate = {
172+
totalAnswers: TotalAnswers
173+
correctAnswer: ChoiceNumber
174+
explanationOverrides?: (ChoiceNumber | null)[] // Tuple<ChoiceNumber, QuestionTemplate["totalAnswers"]>
170175
}
176+
171177
export type QuestionBankConfig = Record<string, QuestionTemplate[]>
172178

173179
export type Answer = {

0 commit comments

Comments
 (0)