Skip to content

Commit a35d509

Browse files
committed
feat: add multiple choice quizzes
1 parent 6385555 commit a35d509

File tree

3 files changed

+81
-10
lines changed

3 files changed

+81
-10
lines changed

components/mdx/Quiz.tsx

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
ModalCloseButton,
1313
useToast,
1414
} from '@chakra-ui/react'
15-
import React, { FC, useState } from 'react'
15+
import React, { FC, useEffect, useState } from 'react'
1616

1717
interface QuizProps {
1818
quiz: string
@@ -34,7 +34,7 @@ interface Quiz {
3434
}
3535

3636
interface Answers {
37-
[index: string]: number
37+
[index: string]: number[]
3838
}
3939

4040
const Quiz: FC<QuizProps> = (props: QuizProps) => {
@@ -65,7 +65,18 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
6565

6666
const selectAnswer = (answerIndex: number) => {
6767
let newAnswers: Answers = { ...answers }
68-
newAnswers[currentQuestionIndex.toString()] = answerIndex
68+
69+
if (newAnswers[currentQuestionIndex]?.includes(answerIndex)) {
70+
newAnswers[currentQuestionIndex.toString()] = newAnswers[
71+
currentQuestionIndex
72+
]?.filter((a) => a !== answerIndex)
73+
} else {
74+
newAnswers[currentQuestionIndex.toString()] = [
75+
...(answers[currentQuestionIndex] || []),
76+
answerIndex,
77+
]
78+
}
79+
6980
setAnswers(newAnswers)
7081
}
7182

@@ -74,12 +85,12 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
7485
correctAnswers &&
7586
correctAnswers.indexOf(currentQuestionIndex) !== -1 &&
7687
quiz.questions[currentQuestionIndex].options[optionIndex].correct &&
77-
answers[currentQuestionIndex] === optionIndex
88+
answers[currentQuestionIndex].includes(optionIndex)
7889
) {
7990
return 'green.500'
8091
}
8192

82-
if (answers[currentQuestionIndex] == optionIndex) {
93+
if (answers[currentQuestionIndex]?.includes(optionIndex)) {
8394
return 'yellow.600'
8495
}
8596
return 'gray.600'
@@ -126,12 +137,21 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
126137
const newCorrectAnswers: number[] = []
127138

128139
quiz.questions.forEach((q, index) => {
129-
if (!q.options[answers[index]].correct) {
130-
hasWrongAnswers = true
131-
wrongAnswersCounter++
132-
} else {
133-
newCorrectAnswers.push(index)
140+
let correctAnswersIndexes = q.options
141+
.filter((option) => option.correct)
142+
.map((correctOption) => q.options.indexOf(correctOption))
143+
144+
for (let i = 0; i < answers[index].length; i++) {
145+
if (answers[index].length >= 2)
146+
answers[index] = answers[index].sort((a, b) => a - b)
147+
if (correctAnswersIndexes[i] !== answers[index][i]) {
148+
hasWrongAnswers = true
149+
wrongAnswersCounter++
150+
return
151+
}
134152
}
153+
154+
newCorrectAnswers.push(index)
135155
})
136156

137157
setCorrectAnswers(newCorrectAnswers)
File renamed without changes.

utils/quizzes/quiz-2.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"title": "Quiz: Lesson 1",
3+
"questions": [
4+
{
5+
"question": "Where do the values for event parameters get stored?",
6+
"options": [
7+
{
8+
"answer": "In the transaction data"
9+
},
10+
{
11+
"answer": "In transaction logs",
12+
"correct": true
13+
},
14+
{
15+
"answer": "They are not stored"
16+
}
17+
]
18+
},
19+
{
20+
"question": "Which of these properties does Ethereum have?",
21+
"options": [
22+
{
23+
"answer": "Permissioned"
24+
},
25+
{
26+
"answer": "Trustless",
27+
"correct": true
28+
},
29+
{
30+
"answer": "Decentralized",
31+
"correct": true
32+
}
33+
]
34+
},
35+
{
36+
"question": "What can we use a smart contract for?",
37+
"options": [
38+
{
39+
"answer": "Store or transfer value or services without the need of a central authority",
40+
"correct": true
41+
},
42+
{
43+
"answer": "Make a breakfast"
44+
},
45+
{
46+
"answer": "Verifying the authenticity of documents and information."
47+
}
48+
]
49+
}
50+
]
51+
}

0 commit comments

Comments
 (0)