function getQuestionsAndAnswers()

in ArticleTemplates/assets/js/modules/quiz.js [138:173]


function getQuestionsAndAnswers() {
    let i;
    let key;
    let answerMatch;
    let question;
    let answer;
    const answers = {};
    const correctAnswerWordList = document.getElementsByClassName('quiz__correct-answers')[0].innerHTML.split(' ');

    for (i = 0; i < correctAnswerWordList.length; i++) {
        // Check if word in this format: 1:A
        answerMatch = correctAnswerWordList[i].match(/(\d+):([A-Z])/g);

        if (answerMatch && answerMatch.length) {
            answer = answerMatch[0];
            question = answer.split(':')[0];
            answers[question] = {
                correctAnswer: answer.split(':')[1]
            };
        } else {
            if (!answers[question].revealText || answers[question].revealText === '- ') {
                answers[question].revealText = '';
            }
            answers[question].revealText += `${correctAnswerWordList[i]} `;
        }
    }

    for (key in answers) {
        if (answers.hasOwnProperty(key) && answers[key].revealText) {
            // Remove trailing comma in revealText
            answers[key].revealText = answers[key].revealText.replace(/,\s*$/, '');
        }
    }

    return answers;
}