-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathQuiz.js
More file actions
70 lines (65 loc) · 2.27 KB
/
Quiz.js
File metadata and controls
70 lines (65 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import React, { useState } from 'react';
import quizData from './quizData';
function Quiz() {
const [currentQuestion, setCurrentQuestion] = useState(0);
const [score, setScore] = useState(0);
const [showScore, setShowScore] = useState(false);
const [selectedAnswer, setSelectedAnswer] = useState("");
const [isCorrect, setIsCorrect] = useState(null);
const handleAnswerOptionClick = (option) => {
const correctAnswer = quizData[currentQuestion].answer;
setSelectedAnswer(option);
if (option === correctAnswer) {
setScore(score + 1);
setIsCorrect(true);
} else {
setIsCorrect(false);
}
// Delay moving to the next question to allow the user to see feedback
setTimeout(() => {
const nextQuestion = currentQuestion + 1;
if (nextQuestion < quizData.length) {
setCurrentQuestion(nextQuestion);
setIsCorrect(null); // Reset for the next question
setSelectedAnswer(""); // Reset selected answer
} else {
setShowScore(true);
}
}, 1000); // Adjust time as needed
};
return (
<div className='quiz'>
{showScore ? (
<div className='score-section'>
You scored {score} out of {quizData.length}
</div>
) : (
<>
<div className='question-section'>
<div className='question-count'>
<span>Question {currentQuestion + 1}</span>/{quizData.length}
</div>
<div className='question-text'>{quizData[currentQuestion].question}</div>
</div>
<div className='answer-section'>
{quizData[currentQuestion].options.map((option) => (
<button
onClick={() => handleAnswerOptionClick(option)}
key={option}
style={{ backgroundColor: selectedAnswer === option ? (isCorrect ? 'lightgreen' : 'pink') : '' }}
>
{option}
</button>
))}
</div>
{selectedAnswer && (
<div style={{ marginTop: '10px' }}>
{isCorrect ? 'Correct! 🎉' : 'Sorry, that’s not right. 😢'}
</div>
)}
</>
)}
</div>
);
}
export default Quiz;