// be in a random order. The order of the choices within each question should also be randomized.
//
function randomizeTest (test) {
- //
- // Your code to randomize questions and answers goes here
- //
+
+ // initialize outputs
+ let questionsReordered = [];
+ let choicesReordered = [];
+ let answersReordered = [];
+
+ // Track whether we've previously chosen a randomly selected question
+ // with an array like [true, false, false, true]
+ let questionChosen = [];
+ for(i=0; i<test.questions.length; i++) {
+ questionChosen.push(false);
+ }
+
+ // Do this until we've chosen every question
+ while(true) {
+ let trialQuestionIndex = Math.floor(Math.random() * test.questions.length);
+ if(questionChosen[trialQuestionIndex]) {
+ continue;
+ }
+ // We've found a question we haven't chosen before
+
+ // Record it so we don't choose it again
+ questionChosen[trialQuestionIndex] = true;
+ // Add the chosen question to the output
+ questionsReordered.push(test.questions[trialQuestionIndex]);
+
+ // initialize the outputs for this question
+ let questionChoices = test.choices[trialQuestionIndex];
+ let questionChoicesReordered = [];
+ let questionAnswers = test.answers[trialQuestionIndex];
+ let questionAnswersReordered = [];
+
+ // Track whether we've previously chosen a randomly selected choice
+ // with an array like [true, false, false, true]
+ let choiceChosen = [];
+ for(i=0; i<questionChoices.length; i++) {
+ choiceChosen.push(false);
+ }
+
+ // Do this until we've chosen every choice for this question
+ while(true) {
+ let trialChoiceIndex = Math.floor(Math.random() * questionChoices.length);
+ if(choiceChosen[trialChoiceIndex]) {
+ continue;
+ }
+ // We've found a choice for this question we haven't chosen before
+
+ // Record it so we don't choose it again
+ choiceChosen[trialChoiceIndex] = true;
+ // Add the chosen choice and answer to the output
+ questionChoicesReordered.push(questionChoices[trialChoiceIndex]);
+ questionAnswersReordered.push(questionAnswers[trialChoiceIndex]);
+
+ // Check if we're done selecting choices for this question
+ let unchosenQuestionChoicesRemain = false;
+ for(i=0; i<choiceChosen.length; i++) {
+ if(!choiceChosen[i]) {
+ unchosenQuestionChoicesRemain = true;
+ }
+ }
+
+ if(unchosenQuestionChoicesRemain) {
+ continue;
+ }
+ break;
+ }
+
+ // Add the choices/answers to the output data structures
+ choicesReordered.push(questionChoicesReordered);
+ answersReordered.push(questionAnswersReordered);
+
+ // Check if we're done selecting questions
+ let unchosenQuestionsRemain = false;
+ for(i=0; i<questionChosen.length; i++) {
+ if(!questionChosen[i]) {
+ unchosenQuestionsRemain = true;
+ }
+ }
+
+ if(unchosenQuestionsRemain) {
+ continue;
+ }
+
+ break;
+ }
+
+ // set modified outputs
+ test.questions = questionsReordered;
+ test.choices = choicesReordered;
+ test.answers = answersReordered;
+
+ return test;
}
function Test (questions, choices, answers) {