Skip to content

Commit 279ea64

Browse files
authored
Merge pull request #14492 from ethereum/quiz-patch
Fix: Custom quiz explanation override [Fixes #14490]
2 parents c639b2a + e9646a8 commit 279ea64

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

src/data/quizzes/questionBank.ts

Lines changed: 37 additions & 7 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,13 +34,32 @@ 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": [
3659
{ totalAnswers: 4, correctAnswer: 2 },
3760
{ totalAnswers: 4, correctAnswer: 1 },
3861
{ totalAnswers: 4, correctAnswer: 4 },
39-
{ totalAnswers: 4, correctAnswer: 1 },
62+
{ totalAnswers: 4, correctAnswer: 1, explanationOverrides: [1, 1, 1, 1] },
4063
{ totalAnswers: 4, correctAnswer: 4 },
4164
],
4265
"what-is-ether": [
@@ -109,11 +132,11 @@ const questionBankConfig: QuestionBankConfig = {
109132
],
110133
"run-a-node": [
111134
{ totalAnswers: 4, correctAnswer: 1 },
112-
{ totalAnswers: 4, correctAnswer: 1 },
135+
{ totalAnswers: 4, correctAnswer: 1, explanationOverrides: [1, 1, 1, 1] },
113136
{ totalAnswers: 4, correctAnswer: 4 },
114137
{ totalAnswers: 4, correctAnswer: 3 },
115138
{ totalAnswers: 4, correctAnswer: 1 },
116-
{ totalAnswers: 2, correctAnswer: 2 },
139+
{ totalAnswers: 2, correctAnswer: 2, explanationOverrides: [1, 1] },
117140
],
118141
stablecoins: [
119142
{ totalAnswers: 4, correctAnswer: 1 },
@@ -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/intl/en/learn-quizzes.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@
5858
"what-is-ethereum-3-d-explanation": "Anyone running a node is a crucial part of Ethereum’s infrastructure. If you haven’t already, consider running an Ethereum node.",
5959
"what-is-ethereum-4-prompt": "Since Ethereum launched, how many times has the network been offline?",
6060
"what-is-ethereum-4-a-label": "Never",
61+
"what-is-ethereum-4-a-explanation": "Ethereum has never gone completely offline (stopped producing blocks) since it launched.",
6162
"what-is-ethereum-4-b-label": "Once",
6263
"what-is-ethereum-4-c-label": "Four times",
6364
"what-is-ethereum-4-d-label": "More than ten times",
64-
"what-is-ethereum-4-explanation": "Ethereum has never gone completely offline (stopped producing blocks) since it launched.",
6565
"what-is-ethereum-5-prompt": "Ethereum consumes more electricity than:",
6666
"what-is-ethereum-5-a-label": "Gold mining",
6767
"what-is-ethereum-5-a-explanation": "Gold mining uses ~131 Terawatt hours per year. Ethereum uses about 0.0026 Terawatt hours per year.",

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)