Skip to content

Instantly share code, notes, and snippets.

@lyellick
Last active October 14, 2023 01:42
Show Gist options
  • Save lyellick/348d4bce02a480999da930cd7e8599e9 to your computer and use it in GitHub Desktop.
Save lyellick/348d4bce02a480999da930cd7e8599e9 to your computer and use it in GitHub Desktop.
Microsoft Practice Exam Choice Randomizer
// What this does:
// - Shuffles the first question's quiz answers.
// - Adds a listener to the next question button and executed the shuffle code after 2 seconds.
shuffleQuizChoices()
document.getElementById("next-button").addEventListener("click", function () {
setTimeout(shuffleQuizChoices, 2000);
});
function shuffleQuizChoices() {
const choicesWrapper = document.querySelector('#main > div > form > div.margin-top-lg.margin-bottom-sm > fieldset > div.control > div');
const choices = Array.from(choicesWrapper.querySelectorAll('.quiz-choice'));
const labels = Array.from(choicesWrapper.querySelectorAll('.margin-left-md'));
choices.forEach(choice => choicesWrapper.removeChild(choice));
labels.forEach(label => choicesWrapper.removeChild(label));
for (let i = choices.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[choices[i], choices[j]] = [choices[j], choices[i]];
[labels[i], labels[j]] = [labels[j], labels[i]];
}
for (let i = choices.length - 1; i > -1; --i) {
choicesWrapper.appendChild(choices[i]);
choicesWrapper.appendChild(labels[i]);
}
console.log("shuffled");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment