I am trying to make an exam that has multiple "pools" of questions that I need to randomly pull a specific number out of and shuffle with the other pools. I have gotten close. I have tried two methods a random splice and a random value. When I use the splice it seems to flatten my 2d array, question and answers. The random value gives me duplicate items.
> <!DOCTYPE html>
>
> <html> <head> <meta charset="utf-8"/> <title>Quiz</title> <style>
> div#test{ border:#000 1px solid; padding:10px 40px 40px 40px; }
> </style>
>
> <script>
> ///PROTOTYPES//////////////////////////////////////////////////////////////
> Array.prototype.randsplice = function() { var ri =
> Math.floor(Math.random() * this.length); //takes it out of the array
> var rs = this.splice(ri, 1); return rs }
>
> Array.prototype.randval = function() { var ri =
> Math.floor(Math.random() * this.length); var val = this[ri]; return
> val; }
>
> Array.prototype.shuffle = function(){
> var i = this.length, j, temp;
> while(--i > 0){
> j = Math.floor(Math.random() * (i+1));
> temp = this[j];
> this[j] = this[i];
> this[i] = temp;
> }
> return this; }
>
> Array.prototype.clean = function(deleteValue) { for (var i = 0; i <
> this.length; i++) {
> if (this[i] == deleteValue) {
> this.splice(i, 1);
> i--;
> } } return this; };
>
> ///PROTOTYPES//////////////////////////////////////////////////////////////
>
>
> var pos = 0, test, test_status, question, choice, choices, chA, chB,
> chC, correct = 0; var questions =[ ["What is 10 +
> 4?","12","14","16","B"], ["What is 20 - 9?","7","13","11","C"],
> ["What is 7 x 3?","21","24","25","A"], ["What is 8 /
> 2?","10","10","4","C"] ]; var questions2 =[ ["What is
> A?","A","B","C","A"], ["What is B?","A","B","C","B"], ["What is
> C?","A","B","C","C"], ["What is D?","B","C","D","C"] ];
>
> function _(x) { return document.getElementById(x); } function
> renderQuestion() { test = _("test"); if(pos >=
> combinedArraysRand.length){ test.innerHTML = "<h2>You got
> "+correct+" of "+combinedArraysRand.length+" questions correct</h2>";
> _("test_status").innerHTML = "Test Completed"; //If you want to
> studnet to rerun test //pos = 0; //correct = 0; return false; }
> _("test_status").innerHTML = "Question "+(pos+1)+" of
> "+combinedArraysRand.length; question = combinedArraysRand[pos][0];
> chA = combinedArraysRand[pos][1]; chB = combinedArraysRand[pos][2];
> chC = combinedArraysRand[pos][3]; test.innerHTML =
> "<h3>"+question+"</h3>"; test.innerHTML += "<input type='radio'
> name='choices' value='A'> "+chA+"<br>"; test.innerHTML += "<input
> type='radio' name='choices' value='B'> "+chB+"<br>"; test.innerHTML
> += "<input type='radio' name='choices' value='C'> "+chC+"<br><br>"; test.innerHTML += "<button onclick='checkAnswer()'>Submit
> Answer</button>"; }
> //////////////////////////////////////////////////////////////////////
>
> var newArray = []; var newArray2 = []; function myFunction() {
> newArray.push(result);
> newArray2.push(result2); return newArray; return newArray2; }
>
> var pool1NumUsed = 3; //var pool2NumUsed = 2; for (i = 0; i <
> pool1NumUsed; i++) { //alert("For loop 1"/*questions.length*/); var
> result = questions.randval(); var result2 = questions2.randval();
> myFunction(); } var resultCleaned = result.clean(); var
> combinedArrays = newArray.concat(newArray2).clean(); var
> combinedArraysRand = combinedArrays.shuffle(); document.write("result
> = "+result); document.write("<hr>"); document.write("result length = "+result.length); document.write("<hr>"); document.write("result2 =
> "+result2); document.write("<hr>"); document.write("result2 length =
> "+result2.length); document.write("<hr>"); document.write("newArray =
> "+newArray);
>
>
> function checkAnswer() { choices =
> document.getElementsByName("choices"); for(var i=0; i<choices.length;
> i++) { if(choices[i].checked){ choice = choices[i].value; } }
> if(choice == combinedArraysRand[pos][4]){ correct++; } pos++;
> renderQuestion(); } window.addEventListener("load", renderQuestion,
> false);
> /////////////////////////////////////////////////////////////////////
> </script> </head> <body> <h2 id="test_status"></h2> <div
> id="test"></div> </body>
> </html>
I need to be able to pull a specific number of questions from each pool, I will have more that 2 pools in the final, and then shuffle them all together. In the end product I will also need to shuffle the answers/distractors (will need to be able to turn this function on and off per question. Questions with numeric answers will need to be in order from lowest to highest.), build a remediation page, and get a score. Any help with this is greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire