Name | Progress |
---|---|
Course outline | Not Read |
Name | Progress |
---|---|
Key Concepts | Not Read |
Project: Pet Parade | Not Read |
Challenge 1 | Not Attempted |
Quiz 1 | Not Attempted |
Name | Progress |
---|---|
Project: Profile Card Creator | Not Read |
Key Concepts | Not Read |
Challenge 2 | Not Attempted |
Quiz 2 | Not Attempted |
Name | Progress |
---|---|
Key Concepts | Not Read |
Project: Magic Garden | Not Read |
Challenge 3 | Not Attempted |
Quiz 3 | Not Attempted |
Name | Progress |
---|---|
Key Concepts | Not Read |
Project: Weather Wardrobe | Not Read |
Challenge 4 | Not Attempted |
Quiz 4 | Not Attempted |
Name | Progress |
---|---|
Key Concepts | Not Read |
Project: "Photo Gallery" | Not Read |
Challenge 6 | Not Attempted |
Quiz 6 | Not Attempted |
Name | Progress |
---|---|
Key Concepts | Not Read |
Project: "Registration Form" | Not Read |
Challenge 7 | Not Read |
Quiz 7 | Not Attempted |
Name | Progress |
---|---|
Key Concepts | Not Read |
Project: "Weather Dashboard" | Not Read |
Challenge 8 | Not Attempted |
Quiz 8 | Not Attempted |
Overview:
In this challenge, you will enhance the "Interactive Quiz App" project by integrating a scoring system that updates as the user selects answers. This will help you practice handling state updates and managing dynamic feedback based on user interactions.
Objective:
Instructions for the Interactive Code Block:
Step 1: Add a Score State
First, you'll need to add a new piece of state to handle the user's score. This will keep track of how many questions the user has answered correctly.
Step 2: Update the Score Based on User's Answer
Modify the <strong>handleSubmit</strong> function to update the score when the user selects the correct answer. Increment the score if the user's selected option matches the correct answer.
Step 3: Display the Score
Add a section to display the user's current score. This will give immediate feedback on their performance.
Step 4: Style the Score Section
Add some basic CSS to style the score display and make it visually appealing.
Step 5: Testing and Refinement
Run your application and test the scoring system. Make sure the score updates correctly as you select answers. Adjust any styling or logic as needed to ensure a smooth user experience.
Next Steps:
By completing this challenge, you will gain a deeper understanding of managing state and dynamic updates in React, making your applications more interactive and user-friendly. Enjoy implementing the scoring system and enhancing your Interactive Quiz App!
Step 1 Hint: Use the <strong>useState</strong> hook to create a state variable called <strong>score</strong>.
// Inside Quiz component
const [score, setScore] = useState(0);
Step 2 Hint: Check if the selected option is correct and use <strong>setScore</strong> to update the score.
function handleSubmit(event) {
event.preventDefault();
const correctAnswer = questions[currentQuestionIndex].answer;
if (selectedOption === correctAnswer) {
setFeedback('Correct!');
setScore((prevScore) => prevScore + 1);
} else {
setFeedback('Incorrect, try again.');
}
}
Step 3 Hint: Use a <strong>div</strong> or <strong>p</strong> tag to display the score and place it inside your component's return statement.
// Inside return statement of Quiz component
<div className="score">
<h3>Score: {score}</h3>
</div>
Step 4 Hint: Create a <strong>.score</strong> class in your CSS file to style the score section.
/* Add to your Quiz.css file */
.score {
margin-top: 20px;
font-size: 20px;
font-weight: bold;
color: green;
}