@@ -12,7 +12,11 @@ import {
12
12
ModalCloseButton ,
13
13
useToast ,
14
14
} from '@chakra-ui/react'
15
- import React , { FC , useEffect , useState } from 'react'
15
+ import React , { FC , useState } from 'react'
16
+ import {
17
+ getCorrectAnswersIndexes ,
18
+ haveSameElements ,
19
+ } from '../../utils/QuizHelpers'
16
20
17
21
interface QuizProps {
18
22
quiz : string
@@ -131,32 +135,24 @@ const Quiz: FC<QuizProps> = (props: QuizProps) => {
131
135
return quizNotAnswered ( )
132
136
}
133
137
134
- let hasWrongAnswers = false
135
138
let wrongAnswersCounter = 0
136
139
137
140
const newCorrectAnswers : number [ ] = [ ]
138
141
139
- quiz . questions . forEach ( ( q , index ) => {
140
- let correctAnswersIndexes = q . options
141
- . filter ( ( option ) => option . correct )
142
- . map ( ( correctOption ) => q . options . indexOf ( correctOption ) )
142
+ quiz . questions . forEach ( ( question , index ) => {
143
+ let correctAnswersIndexes = getCorrectAnswersIndexes ( question )
143
144
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
- }
145
+ if ( ! haveSameElements ( answers [ index ] , correctAnswersIndexes ) ) {
146
+ wrongAnswersCounter ++
147
+ return
152
148
}
153
149
154
150
newCorrectAnswers . push ( index )
155
151
} )
156
152
157
153
setCorrectAnswers ( newCorrectAnswers )
158
154
159
- if ( hasWrongAnswers ) {
155
+ if ( wrongAnswersCounter >= 1 ) {
160
156
return quizFailedToast ( wrongAnswersCounter )
161
157
}
162
158
0 commit comments