Skip to content

Commit

Permalink
fix(error ) - QCMPLUS-38 : canculate score
Browse files Browse the repository at this point in the history
Signed-off-by: teklit_tewolde <[email protected]>
  • Loading branch information
teklit_tewolde committed Aug 20, 2024
1 parent 208f82d commit 4542ac5
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions qcmplusweb/src/components/Exam/Exam.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,17 @@ const Exam = ({ quizId }) => {
const [timer, setTimer] = useState(QUESTION_TIME_LIMIT);
const [score, setScore] = useState(0);

useEffect(() => {
const startQuiz = async () => {
setLoading(true);
try {
const response = await getQuestions(quizId);
if (response && response.data) {
const selectedQuestions = response.data.slice(0, MAX_QUESTIONS);
setQuestions(selectedQuestions);
setCurrentQuestionIndex(0);
setUserAnswers({});
setExamCompleted(false);
setTimer(QUESTION_TIME_LIMIT);
} else {
setError('No questions found for this quiz.');
}
} catch (error) {
console.error('Error fetching questions:', error);
setError('An error occurred while fetching questions.');
} finally {
setLoading(false);
// Move the calculateScore function above the useCallback hooks
const calculateScore = useCallback(() => {
let score = 0;
questions.forEach((question) => {
const correctAnswer = answers[question.questionId]?.find(answer => answer.isCorrect);
if (correctAnswer && userAnswers[question.questionId] === correctAnswer.answerId) {
score += 1;
}
};

if (quizId) {
startQuiz();
}
}, [quizId]);
});
return score;
}, [questions, answers, userAnswers]);

const handleSubmit = useCallback(async () => {
const sessionData = {
Expand All @@ -57,7 +41,7 @@ const Exam = ({ quizId }) => {
};
try {
await submitExamSession(sessionData);
const calculatedScore = calculateScore();
const calculatedScore = calculateScore(); // No warning now because calculateScore is defined before
setScore(calculatedScore);
setExamCompleted(true);
setShowResults(true);
Expand All @@ -75,6 +59,33 @@ const Exam = ({ quizId }) => {
}
}, [currentQuestionIndex, questions.length, handleSubmit]);

useEffect(() => {
const startQuiz = async () => {
setLoading(true);
try {
const response = await getQuestions(quizId);
if (response && response.data) {
const selectedQuestions = response.data.slice(0, MAX_QUESTIONS);
setQuestions(selectedQuestions);
setCurrentQuestionIndex(0);
setUserAnswers({});
setExamCompleted(false);
setTimer(QUESTION_TIME_LIMIT);
} else {
setError('No questions found for this quiz.');
}
} catch (error) {
console.error('Error fetching questions:', error);
setError('An error occurred while fetching questions.');
} finally {
setLoading(false);
}
};

if (quizId) {
startQuiz();
}
}, [quizId]);

useEffect(() => {
if (questions.length > 0 && timer > 0) {
Expand Down Expand Up @@ -122,17 +133,6 @@ const Exam = ({ quizId }) => {
}
};

const calculateScore = () => {
let score = 0;
questions.forEach((question) => {
const correctAnswer = answers[question.questionId]?.find(answer => answer.isCorrect);
if (correctAnswer && userAnswers[question.questionId] === correctAnswer.answerId) {
score += 1;
}
});
return score;
};

if (loading) {
return (
<div className="d-flex justify-content-center align-items-center" style={{ height: '100vh' }}>
Expand Down

0 comments on commit 4542ac5

Please sign in to comment.