I have a post Api in which I want to send the answers based on users input in an array like :
userAnswer{“correct”, “correct”, “wrong”, “notAttempted”, “correct”} and then replace the correct and wrong answers based on their index in the next section in a different array that is like this :
{
“answersArray”: [
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”,
“notAttempted”
],
}
const [arry, setArry] = useState([]);
const nextQuestion = (examData) => {
if (studentAnswered == examData[currentQuestion].correct) {
setArry((prevState) => [...arry, "correct"]);
} else if (studentAnswered == null) {
setArry((prevState) => [...arry, "notAttempted"]);
} else {
setArry((prevState) => [...arry, "wrong"]);
}
setCurrentQuestion(currentQuestion + 1);
setStudentAnswered(null);
};
The array is a useState Function and NextQuestion is an onClick function it adds the selected answer to the array onClick or keeps the value null if no options are selected. **I was able to do most of the work. Now I just need to update the correct and wrong answers to the AnswersArray Above based on their indexes. Thanks **