@@ -88,39 +88,30 @@ const SelectionSortQuiz = () => {
8888 const [ selectedAnswer , setSelectedAnswer ] = useState ( null ) ;
8989 const [ score , setScore ] = useState ( 0 ) ;
9090 const [ showResult , setShowResult ] = useState ( false ) ;
91- const [ setQuizCompleted ] = useState ( false ) ;
91+ const [ quizCompleted , setQuizCompleted ] = useState ( false ) ;
9292 const [ answers , setAnswers ] = useState ( Array ( questions . length ) . fill ( null ) ) ;
93- const [ showExplanation , setShowExplanation ] = useState ( false ) ;
9493 const [ showIntro , setShowIntro ] = useState ( true ) ;
9594 const [ showSuccessAnimation , setShowSuccessAnimation ] = useState ( false ) ;
96- const [ penaltyApplied , setPenaltyApplied ] = useState ( false ) ;
9795
9896 const handleAnswerSelect = ( optionIndex ) => {
99- if ( selectedAnswer !== null ) return ;
10097 setSelectedAnswer ( optionIndex ) ;
101- const newAnswers = [ ...answers ] ;
102- newAnswers [ currentQuestion ] = optionIndex ;
103- setAnswers ( newAnswers ) ;
10498 } ;
10599
106100 const handleNextQuestion = ( ) => {
107101 if ( selectedAnswer === null ) return ;
108-
109- if ( showExplanation && ! penaltyApplied ) {
110- setScore ( prevScore => Math . max ( 0 , prevScore - 0.5 ) ) ;
111- setPenaltyApplied ( true ) ;
112- }
113102
114- if ( selectedAnswer === questions [ currentQuestion ] . correctAnswer ) {
115- setScore ( score + 1 ) ;
116- }
117-
118- setShowExplanation ( false ) ;
119- setPenaltyApplied ( false ) ;
120-
103+ const newAnswers = [ ...answers ] ;
104+ newAnswers [ currentQuestion ] = selectedAnswer ;
105+ setAnswers ( newAnswers ) ;
106+
107+ const newScore = newAnswers . reduce ( ( acc , ans , idx ) => {
108+ return ans === questions [ idx ] . correctAnswer ? acc + 1 : acc ;
109+ } , 0 ) ;
110+ setScore ( newScore ) ;
111+
121112 if ( currentQuestion < questions . length - 1 ) {
122113 setCurrentQuestion ( currentQuestion + 1 ) ;
123- setSelectedAnswer ( null ) ;
114+ setSelectedAnswer ( newAnswers [ currentQuestion + 1 ] ) ;
124115 } else {
125116 setShowSuccessAnimation ( true ) ;
126117 setTimeout ( ( ) => {
@@ -132,7 +123,6 @@ const SelectionSortQuiz = () => {
132123 } ;
133124
134125 const handlePreviousQuestion = ( ) => {
135- setShowExplanation ( false ) ;
136126 setCurrentQuestion ( currentQuestion - 1 ) ;
137127 setSelectedAnswer ( answers [ currentQuestion - 1 ] ) ;
138128 } ;
@@ -144,9 +134,7 @@ const SelectionSortQuiz = () => {
144134 setShowResult ( false ) ;
145135 setQuizCompleted ( false ) ;
146136 setAnswers ( Array ( questions . length ) . fill ( null ) ) ;
147- setShowExplanation ( false ) ;
148137 setShowIntro ( true ) ;
149- setPenaltyApplied ( false ) ;
150138 } ;
151139
152140 const calculateWeakAreas = ( ) => {
@@ -220,10 +208,6 @@ const SelectionSortQuiz = () => {
220208 < FaTimes className = "text-blue-500 mt-1 mr-2 flex-shrink-0" />
221209 < span > 0 points for wrong answers</ span >
222210 </ li >
223- < li className = "flex items-start" >
224- < FaInfoCircle className = "text-blue-500 mt-1 mr-2 flex-shrink-0" />
225- < span > -0.5 point penalty for viewing explanations</ span >
226- </ li >
227211 < li className = "flex items-start" >
228212 < FaTrophy className = "text-blue-500 mt-1 mr-2 flex-shrink-0" />
229213 < span > Earn stars based on your final score (max 5 stars)</ span >
@@ -325,29 +309,6 @@ const SelectionSortQuiz = () => {
325309 ) ) }
326310 </ div >
327311
328- { selectedAnswer !== null && (
329- < div className = "mb-6" >
330- < button
331- onClick = { ( ) => setShowExplanation ( ! showExplanation ) }
332- className = "text-sm flex items-center text-blue-600 dark:text-blue-400 hover:underline mb-2"
333- >
334- < FaInfoCircle className = "mr-1" />
335- { showExplanation ? "Hide Explanation" : "Show Explanation" }
336- </ button >
337- < AnimatePresence >
338- { showExplanation && (
339- < motion . div
340- initial = { { opacity : 0 , height : 0 } }
341- animate = { { opacity : 1 , height : "auto" } }
342- exit = { { opacity : 0 , height : 0 } }
343- className = "p-4 bg-blue-50 dark:bg-blue-900/20 rounded-lg text-sm overflow-hidden"
344- >
345- { questions [ currentQuestion ] . explanation }
346- </ motion . div >
347- ) }
348- </ AnimatePresence >
349- </ div >
350- ) }
351312 </ motion . div >
352313
353314 < div className = "flex justify-between" >
0 commit comments