@@ -12,7 +12,7 @@ import {
12
12
ModalCloseButton ,
13
13
useToast ,
14
14
} from '@chakra-ui/react'
15
- import React , { FC , useState } from 'react'
15
+ import React , { FC , useEffect , useState } from 'react'
16
16
17
17
interface QuizProps {
18
18
quiz : string
@@ -34,7 +34,7 @@ interface Quiz {
34
34
}
35
35
36
36
interface Answers {
37
- [ index : string ] : number
37
+ [ index : string ] : number [ ]
38
38
}
39
39
40
40
const Quiz : FC < QuizProps > = ( props : QuizProps ) => {
@@ -65,7 +65,18 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
65
65
66
66
const selectAnswer = ( answerIndex : number ) => {
67
67
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
+
69
80
setAnswers ( newAnswers )
70
81
}
71
82
@@ -74,12 +85,12 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
74
85
correctAnswers &&
75
86
correctAnswers . indexOf ( currentQuestionIndex ) !== - 1 &&
76
87
quiz . questions [ currentQuestionIndex ] . options [ optionIndex ] . correct &&
77
- answers [ currentQuestionIndex ] === optionIndex
88
+ answers [ currentQuestionIndex ] . includes ( optionIndex )
78
89
) {
79
90
return 'green.500'
80
91
}
81
92
82
- if ( answers [ currentQuestionIndex ] == optionIndex ) {
93
+ if ( answers [ currentQuestionIndex ] ?. includes ( optionIndex ) ) {
83
94
return 'yellow.600'
84
95
}
85
96
return 'gray.600'
@@ -126,12 +137,21 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
126
137
const newCorrectAnswers : number [ ] = [ ]
127
138
128
139
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
+ }
134
152
}
153
+
154
+ newCorrectAnswers . push ( index )
135
155
} )
136
156
137
157
setCorrectAnswers ( newCorrectAnswers )
0 commit comments