diff --git a/client/src/components/QuestionsDisplay/MultipleChoiceQuestionDisplay/MultipleChoiceQuestionDisplay.tsx b/client/src/components/QuestionsDisplay/MultipleChoiceQuestionDisplay/MultipleChoiceQuestionDisplay.tsx
index 829752d..d023a40 100644
--- a/client/src/components/QuestionsDisplay/MultipleChoiceQuestionDisplay/MultipleChoiceQuestionDisplay.tsx
+++ b/client/src/components/QuestionsDisplay/MultipleChoiceQuestionDisplay/MultipleChoiceQuestionDisplay.tsx
@@ -15,7 +15,14 @@ interface Props {
const MultipleChoiceQuestionDisplay: React.FC
= (props) => {
const { question, showAnswer, handleOnSubmitAnswer, passedAnswer } = props;
- const [answer, setAnswer] = useState(passedAnswer || []);
+ console.log('MultipleChoiceQuestionDisplay: passedAnswer', JSON.stringify(passedAnswer));
+
+ const [answer, setAnswer] = useState(() => {
+ if (passedAnswer && passedAnswer.length > 0) {
+ return passedAnswer;
+ }
+ return [];
+ });
let disableButton = false;
if (handleOnSubmitAnswer === undefined) {
@@ -23,19 +30,31 @@ const MultipleChoiceQuestionDisplay: React.FC = (props) => {
}
useEffect(() => {
+ console.log('MultipleChoiceQuestionDisplay: passedAnswer', JSON.stringify(passedAnswer));
if (passedAnswer !== undefined) {
setAnswer(passedAnswer);
+ } else {
+ setAnswer([]);
}
- }, [passedAnswer]);
+ }, [passedAnswer, question.id]);
const handleOnClickAnswer = (choice: string) => {
setAnswer((prevAnswer) => {
- if (prevAnswer.includes(choice)) {
- // Remove the choice if it's already selected
- return prevAnswer.filter((selected) => selected !== choice);
+ console.log(`handleOnClickAnswer -- setAnswer(): prevAnswer: ${prevAnswer}, choice: ${choice}`);
+ const correctAnswersCount = question.choices.filter((c) => c.isCorrect).length;
+
+ if (correctAnswersCount === 1) {
+ // If only one correct answer, replace the current selection
+ return prevAnswer.includes(choice) ? [] : [choice];
} else {
- // Add the choice if it's not already selected
- return [...prevAnswer, choice];
+ // Allow multiple selections if there are multiple correct answers
+ if (prevAnswer.includes(choice)) {
+ // Remove the choice if it's already selected
+ return prevAnswer.filter((selected) => selected !== choice);
+ } else {
+ // Add the choice if it's not already selected
+ return [...prevAnswer, choice];
+ }
}
});
};
@@ -50,6 +69,7 @@ const MultipleChoiceQuestionDisplay: React.FC = (props) => {
{question.choices.map((choice, i) => {
+ console.log(`answer: ${answer}, choice: ${choice.formattedText.text}`);
const selected = answer.includes(choice.formattedText.text) ? 'selected' : '';
return (
diff --git a/client/src/pages/Teacher/ManageRoom/useRooms.ts b/client/src/pages/Teacher/ManageRoom/useRooms.ts
index 6be117c..2dadbfb 100644
--- a/client/src/pages/Teacher/ManageRoom/useRooms.ts
+++ b/client/src/pages/Teacher/ManageRoom/useRooms.ts
@@ -55,14 +55,15 @@ export function checkIfIsCorrect(
(!question.isTrue && simpleAnswerText == 'false')
);
} else if (question.type === 'MC') {
- const correctAnswers = question.choices.filter((choice) => choice.isCorrect
+ const correctChoices = question.choices.filter((choice) => choice.isCorrect
/* || (choice.weight && choice.weight > 0)*/ // handle weighted answers
);
const multipleAnswers = Array.isArray(answer) ? answer : [answer as string];
- if (correctAnswers.length === 0) {
+ if (correctChoices.length === 0) {
return false;
}
- return correctAnswers.every(
+ // check if all (and only) correct choices are in the multipleAnswers array
+ return correctChoices.length === multipleAnswers.length && correctChoices.every(
(choice) => multipleAnswers.includes(choice.formattedText.text)
);
} else if (question.type === 'Numerical') {