CCol className=col-md-8 result-maintain h3 All Question Type h3 div className=mt-4 mb-0 h5 classN
To have one array for MCQ and True/False questions and run them in a single loop, you can create an array of objects where each object represents a question. Each object can have properties like "question type", "question statement", "options", "selected answer", and "correct answer".
Here's an example:
const questions = [
{
questionType: "MCQ",
question: "What is a blockchain?",
options: [
"A. A Currency",
"B. A centralized ledger",
"C. A type of cryptocurrency",
"D. A distributed ledger on a peer to peer network"
],
selectedAnswer: "", // Can be used to store the user's selected answer
correctAnswer: "B" // The correct answer
},
{
questionType: "True/False",
question: "The fs.read() method is used to read files on your computer.",
options: [
"A. True",
"B. False"
],
selectedAnswer: "", // Can be used to store the user's selected answer
correctAnswer: "B" // The correct answer
},
// Add more questions here
];
Then, you can iterate over the questions array and display the questions and options dynamically based on the question type. You can also check the user's selected answer against the correct answer to determine if it is correct or not.
Here's an example of how you can iterate over the questions array using a single loop:
{questions.map((question, index) => (
<div key={index}>
<h5 className="mt-4 mb-0">{question.questionType}</h5>
<hr/>
<strong>{index+1}.{question.question}</strong>
{question.options.map((option, optionIndex) => (
<p key={optionIndex}><strong>{option}</strong></p>
))}
<div className="d-grid gap-2 d-md-block">
<CDropdown variant="btn-group">
<CDropdownToggle color="success">Show Answer</CDropdownToggle>
<CDropdownMenu>
<CDropdownItem href="#">Selected Answer: {question.selectedAnswer}</CDropdownItem>
<CDropdownDivider />
<CDropdownItem href="#">Correct Answer: {question.correctAnswer}</CDropdownItem>
</CDropdownMenu>
</CDropdown>
</div>
</div>
))}
In this example, we use the map function to iterate over the questions array and render the question and options dynamically. The questionType property is used to display the question type, and the options array is iterated over to display the options. The selectedAnswer and correctAnswer properties are used to display the selected and correct answers in the dropdown menu
原文地址: https://www.cveoy.top/t/topic/hVBq 著作权归作者所有。请勿转载和采集!