import {FillBlanksExercise} from "@/interfaces/exam"; import clsx from "clsx"; import reactStringReplace from "react-string-replace"; import {CommonProps} from "."; import {Fragment} from "react"; import Button from "../Low/Button"; export default function FillBlanksSolutions({ id, type, prompt, solutions, words, text, userSolutions, onNext, onBack, }: FillBlanksExercise & CommonProps) { const calculateScore = () => { const total = text.match(/({{\d+}})/g)?.length || 0; const correct = userSolutions.filter((x) => { const solution = solutions.find((y) => x.id.toString() === y.id.toString())?.solution.toLowerCase(); if (!solution) return false; const option = words.find((w) => typeof w === "string" ? w.toLowerCase() === x.solution.toLowerCase() : w.letter.toLowerCase() === x.solution.toLowerCase(), ); if (!option) return false; return solution === (typeof option === "string" ? option.toLowerCase() : option.word.toLowerCase()); }).length; const missing = total - userSolutions.filter((x) => solutions.find((y) => x.id.toString() === y.id.toString())).length; return {total, correct, missing}; }; const renderLines = (line: string) => { return ( {reactStringReplace(line, /({{\d+}})/g, (match) => { const id = match.replaceAll(/[\{\}]/g, ""); const userSolution = userSolutions.find((x) => x.id === id); const solution = solutions.find((x) => x.id === id)!; if (!userSolution) { return ( ); } const userSolutionWord = words.find((w) => typeof w === "string" ? w.toLowerCase() === userSolution.solution.toLowerCase() : w.letter.toLowerCase() === userSolution.solution.toLowerCase(), ); const userSolutionText = typeof userSolutionWord === "string" ? userSolutionWord : userSolutionWord?.word; if (userSolutionText === solution.solution) { return ( ); } if (userSolutionText !== solution.solution) { return ( <> > ); } })} ); }; return ( <>
{renderLines(line)}