72 lines
1.9 KiB
JavaScript
72 lines
1.9 KiB
JavaScript
|
/**
|
||
|
*
|
||
|
* @param {Array<Answer>} answerData
|
||
|
* @returns
|
||
|
*/
|
||
|
function answerQuestion(answerData){
|
||
|
const question = document.querySelector(".question:not(.hidden)");
|
||
|
if (!question) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const questionTextDom = question.querySelector(".questionText .mattext");
|
||
|
if (!questionTextDom) return;
|
||
|
const questionText = questionTextDom.textContent.trim();
|
||
|
|
||
|
const answersDom = question.querySelector("ul.coreContent");
|
||
|
if (!answersDom) return;
|
||
|
const answers = answersDom.children;
|
||
|
|
||
|
for (let answer of Array.from(answers)) {
|
||
|
const input = answer.querySelector("input");
|
||
|
if (!input) continue;
|
||
|
input.checked = false;
|
||
|
}
|
||
|
|
||
|
const correctAnswers = findAnswers(answerData, questionText, answers);
|
||
|
if (correctAnswers.length === 0) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
for (const answer of correctAnswers) {
|
||
|
const input = answer.querySelector("input");
|
||
|
if (!input) continue;
|
||
|
input.checked = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param {Array<Answer>} answerData
|
||
|
* @param {string} questionText
|
||
|
* @returns
|
||
|
*/
|
||
|
function findAnswers(answerData, questionText, answers) {
|
||
|
if (answerData === null) {
|
||
|
return [];
|
||
|
}
|
||
|
|
||
|
const correctAnswers = [];
|
||
|
for (let entry of answerData) {
|
||
|
if (matchAnswer(questionText.trim(), entry.question.trim())) {
|
||
|
for (let availableAnswer of answers) {
|
||
|
for (let possibleAnswer of entry.answers) {
|
||
|
if (matchAnswer(availableAnswer.textContent.trim(), possibleAnswer)) {
|
||
|
correctAnswers.push(availableAnswer);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return correctAnswers;
|
||
|
}
|
||
|
|
||
|
function matchAnswer(textA, textB) {
|
||
|
const replaceRegex = /[^\w]/gi;
|
||
|
textA = textA.replace(replaceRegex, "");
|
||
|
textB = textB.replace(replaceRegex, "");
|
||
|
return (textA === textB);
|
||
|
}
|
||
|
|
||
|
window.answerQuestion = answerQuestion;
|