Initial working commit
Some checks failed
Automated Container Build / build-and-push (push) Failing after 7s
Some checks failed
Automated Container Build / build-and-push (push) Failing after 7s
This commit is contained in:
parent
666ceb7325
commit
b7ce314f01
105 changed files with 35510 additions and 11 deletions
41
src/lib/scoring.ts
Normal file
41
src/lib/scoring.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
type OptionLite = { id: string; isCorrect: boolean };
|
||||
type QuestionLite = {
|
||||
id: string;
|
||||
type: "MULTIPLE_CHOICE" | "SATA";
|
||||
options: OptionLite[];
|
||||
};
|
||||
|
||||
export function scoreQuestion(
|
||||
question: QuestionLite,
|
||||
selectedIds: string[]
|
||||
): number {
|
||||
const correctIds = question.options
|
||||
.filter((o) => o.isCorrect)
|
||||
.map((o) => o.id);
|
||||
|
||||
if (question.type === "MULTIPLE_CHOICE") {
|
||||
return correctIds.includes(selectedIds[0]) ? 1 : 0;
|
||||
}
|
||||
|
||||
// SATA: partial credit
|
||||
const correctSelected = selectedIds.filter((id) =>
|
||||
correctIds.includes(id)
|
||||
).length;
|
||||
const incorrectSelected = selectedIds.filter(
|
||||
(id) => !correctIds.includes(id)
|
||||
).length;
|
||||
return Math.max(0, correctSelected - incorrectSelected) / correctIds.length;
|
||||
}
|
||||
|
||||
export function scoreQuiz(
|
||||
questions: QuestionLite[],
|
||||
answers: Record<string, string[]>
|
||||
) {
|
||||
let total = 0;
|
||||
const perQuestion = questions.map((q) => {
|
||||
const points = scoreQuestion(q, answers[q.id] ?? []);
|
||||
total += points;
|
||||
return { questionId: q.id, points };
|
||||
});
|
||||
return { total, maxScore: questions.length, perQuestion };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue