Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix attemp #45

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/modes/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const Modes: NextPage<{ searchParams: { url: string; name: string } }> = ({
<ExamLink
href={{
pathname: "/practice",
query: { url },
query: { url, name },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for aligning it with the root page query

}}
heading="Practice mode"
paragraph="Learn and familiarize yourself with the questions and answers without any time constraint."
Expand All @@ -30,7 +30,7 @@ const Modes: NextPage<{ searchParams: { url: string; name: string } }> = ({
<ExamLink
href={{
pathname: "/exam",
query: { url },
query: { url, name },
}}
heading="Exam mode"
paragraph="Put your knowledge to the test by answering a fixed number of randomly selected questions under a time
Expand Down
10 changes: 2 additions & 8 deletions components/QuizExamForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ const QuizExamForm: React.FC<Props> = ({
images,
}) => {
const [showCorrectAnswer, setShowCorrectAnswer] = useState<boolean>(false);
const [savedAnswers, setSavedAnswers] = useState<any>([]);
const { points, reCount } = useResults(savedAnswers);
const { points, savedAnswers, setSavedAnswers } = useResults(questions);
const [selectedImage, setSelectedImage] = useState<{
url: string;
alt: string;
Expand All @@ -65,12 +64,7 @@ const QuizExamForm: React.FC<Props> = ({
],
},
onSubmit: () => {
saveAnswers(false).then(() => {
reCount({
questions: questions,
answers: savedAnswers,
});
});
saveAnswers(false);
stopTimer();
},
});
Expand Down
48 changes: 26 additions & 22 deletions hooks/useResults.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
import { Question } from "@azure-fundamentals/components/types";
import { useState } from "react";

export type ResultsData = {
questions: Question[];
answers: [] | (string | string[] | null)[];
};
import { useState, useEffect } from "react";

export type ResultsHook = {
points: number;
reCount: (data: ResultsData) => void;
setSavedAnswers: (data: any[]) => void;
savedAnswers: any;
};

const useResults = (data: ResultsData): ResultsHook => {
const useResults = (questions: Question[]): ResultsHook => {
const [points, setPoints] = useState(0);
const [savedAnswers, setSavedAnswers] = useState<any>([]);

const countPoints = (data: ResultsData) => {
const countPoints = () => {
let points = 0;
for (let i = 0; i < data.questions?.length; i++) {
if (!data.answers[i] || !data.questions[i]) continue;
const noOfAnswers = data.questions[i]?.options
? data.questions[i]?.options?.filter((el: any) => el.isAnswer === true)
for (let i = 0; i < questions?.length; i++) {
if (!savedAnswers[i] || !questions[i]) continue;
const noOfAnswers = questions[i]?.options
? questions[i]?.options?.filter((el: any) => el.isAnswer === true)
.length
: 0;
if (noOfAnswers > 1) {
let partialPoints = 0;
const pointRaise = Math.round((1 / noOfAnswers) * 100) / 100;
let isOneBad = false;
let pointRaisedCount = 0;
if (Array.isArray(data.answers[i])) {
for (const answer of data.answers[i] ?? []) {
if (Array.isArray(savedAnswers[i])) {
for (const answer of savedAnswers[i] ?? []) {
if (
data.questions[i]?.options
questions[i]?.options
?.filter((el: any) => el.isAnswer === true)
.some((el: any) => el.text === answer)
) {
partialPoints = partialPoints + pointRaise;
pointRaisedCount = pointRaisedCount + 1;
}
if (
data.questions[i]?.options
questions[i]?.options
?.filter((el: any) => el.isAnswer === false)
.some((el: any) => el.text === answer)
) {
Expand All @@ -55,11 +52,10 @@ const useResults = (data: ResultsData): ResultsHook => {
(partialPoints > 0 ? Math.round(partialPoints * 100) / 100 : 0);
points = Math.round(points * 100) / 100;
}
} else if (noOfAnswers === 1 && !Array.isArray(data.answers[i])) {
} else if (noOfAnswers === 1 && !Array.isArray(savedAnswers[i])) {
if (
data.questions[i]?.options?.filter(
(el: any) => el.isAnswer === true,
)[0]?.text === data.answers[i]
questions[i]?.options?.filter((el: any) => el.isAnswer === true)[0]
?.text === savedAnswers[i]
) {
points = Math.round((points + 1) * 100) / 100;
}
Expand All @@ -68,9 +64,17 @@ const useResults = (data: ResultsData): ResultsHook => {
setPoints(points);
};

// Trigger countPoints when savedAnswers is updated
useEffect(() => {
if (savedAnswers.length === questions.length) {
countPoints();
}
}, [savedAnswers]); // Depend on savedAnswers to trigger the effect

return {
points: points,
reCount: countPoints,
setSavedAnswers: setSavedAnswers,
savedAnswers: savedAnswers,
};
};

Expand Down