Skip to content

Commit 4beaf56

Browse files
Refactor function
1 parent e2c46b3 commit 4beaf56

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

components/QuizExamFormUF.tsx

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,29 @@ const QuizExamForm: FC<Props> = ({
102102

103103
const nextQuestion = (skip: boolean) => {
104104
saveAnswers(skip);
105-
let areAllQuestionsAnswered = false;
106-
let i = currentQuestionIndex + 1;
107-
while (savedAnswers[i] !== null && i < totalQuestions) {
108-
i++;
109-
}
110-
if (i >= totalQuestions) {
111-
i = 0;
112-
}
113-
while (savedAnswers[i] !== null && i < totalQuestions) {
114-
i++;
105+
106+
let nextIndex = currentQuestionIndex + 1;
107+
108+
while (savedAnswers[nextIndex] !== null && nextIndex < totalQuestions) {
109+
nextIndex++;
115110
}
116-
if (i >= totalQuestions) {
117-
areAllQuestionsAnswered = true;
111+
112+
if (nextIndex >= totalQuestions) {
113+
nextIndex = 0;
114+
while (savedAnswers[nextIndex] !== null && nextIndex < totalQuestions) {
115+
nextIndex++;
116+
}
118117
}
119-
if (skip === true) {
120-
handleSkipQuestion(i);
118+
119+
const areAllQuestionsAnswered = nextIndex >= totalQuestions;
120+
121+
if (skip) {
122+
handleSkipQuestion(nextIndex);
123+
} else if (areAllQuestionsAnswered) {
124+
handleSubmit(onSubmit)();
121125
} else {
122-
if (areAllQuestionsAnswered) {
123-
handleSubmit(onSubmit)();
124-
} else {
125-
handleCountAnswered();
126-
handleNextQuestion(i);
127-
}
126+
handleCountAnswered();
127+
handleNextQuestion(nextIndex);
128128
}
129129
};
130130

0 commit comments

Comments
 (0)