/    Sign up×
Community /Pin to ProfileBookmark

generating random questions from a pool

hello…so being a new to html i’m to make offline web based questionaires with 50 questions from a pool of 150 questions (randomly generated)
i’ve read this thread “http://www.webdeveloper.com/forum/showthread.php?286477-How-to-make-a-javascript-quiz-to-choose-10-random-questions-from-100-questions“,

but since i have questions i want to ask… on my test code below…how can i do it (for example 5 questions out 10) ?
—————————–

<html><head><title></title>
<script language=”JavaScript”>
<!–

var numQues = 10;
var numChoi = 4;

var answers = new Array(10);
answers[0] = “blue”;
answers[1] = “dog”;
answers[2] = “C”;
answers[3] = “2”;
answers[4] = “Athens”;
answers[5] = “thames”;
answers[6] = “kilimantzaro”;
answers[7] = “France”;
answers[8] = “Oceania”;
answers[9] = “rose”;

function getScore(form) {
var score = 0;
var currElt;
var currSelection;

for (i=0; i<numQues; i++) {
currElt = i*numChoi;
for (j=0; j<numChoi; j++) {
currSelection = form.elements[currElt + j];
if (currSelection.checked) {
if (currSelection.value == answers[i]) {
score++;
break;
}
}
}
}

score = Math.round(score/numQues*100);
form.percentage.value = score + “%”;

var correctAnswers = “”;
for (i=1; i<=numQues; i++) {
correctAnswers += i + “. ” + answers[i-1] + “rn”;
}
form.solutions.value = correctAnswers;

}

// –>
</script>
</head>

<body>

<form name=”quiz”>
1. name a colour<br>
<input type=”radio” name=”q1″ value=”blue”>blue<br>
<input type=”radio” name=”q1″ value=”car”>car<br>
<input type=”radio” name=”q1″ value=”chair”>chair<br>
<input type=”radio” name=”q1″ value=”sky”>sky<br>
<p>

  • 2.

    name an animal<br>
    <input type=”radio” name=”q2″ value=”yellow”>yellow<br>
    <input type=”radio” name=”q2″ value=”red”>red<br>
    <input type=”radio” name=”q2″ value=”dog”>dog<br>
    <input type=”radio” name=”q2″ value=”bullet”>bullet<br>
    <p>

  • 3.

    name a letter<br>
    <input type=”radio” name=”q3″ value=”2″>2<br>
    <input type=”radio” name=”q3″ value=”bird”>bird<br>
    <input type=”radio” name=”q3″ value=”4″>4<br>
    <input type=”radio” name=”q3″ value=”C”>C<br>
    <p>

  • 4.

    name a number<br>
    <input type=”radio” name=”q4″ value=”2″>2<br>
    <input type=”radio” name=”q4″ value=”D”>D<br>
    <input type=”radio” name=”q4″ value=”plane”>plane<br>
    <input type=”radio” name=”q4″ value=”Brazil”>Brazil<br>
    <p>

  • 5.

    name a city<br>
    <input type=”radio” name=”q5″ value=”amazon”>amazon<br>
    <input type=”radio” name=”q5″ value=”everest”>everest<br>
    <input type=”radio” name=”q5″ value=”Athens”>Athens<br>
    <input type=”radio” name=”q5″ value=”John”>John<br>
    <p>

  • 6.

    name a river<br>
    <input type=”radio” name=”q6″ value=”thames”>thames<br>
    <input type=”radio” name=”q6″ value=”china”>china<br>
    <input type=”radio” name=”q6″ value=”africa”>africa<br>
    <input type=”radio” name=”q6″ value=”7″>7<br>
    <p>

  • 7.

    name a mountain<br>
    <input type=”radio” name=”q7″ value=”steven”>steven<br>
    <input type=”radio” name=”q7″ value=”kilimantzaro”>kilimantzaro<br>
    <input type=”radio” name=”q7″ value=”rome”>rome<br>
    <input type=”radio” name=”q7″ value=”bear”>bear<br>
    <p>

  • 8.

    name a country<br>
    <input type=”radio” name=”q8″ value=”France”>France<br>
    <input type=”radio” name=”q8″ value=”atlantic”>atlantic<br>
    <input type=”radio” name=”q8″ value=”jaguar”>jaguar<br>
    <input type=”radio” name=”q8″ value=”G”>G<br>
    <p>

  • 9.

    name a continent<br>
    <input type=”radio” name=”q9″ value=”7″>7<br>
    <input type=”radio” name=”q9″ value=”James”>James<br>
    <input type=”radio” name=”q9″ value=”Oceania”>Oceania<br>
    <input type=”radio” name=”q9″ value=”snake”>snake<br>
    <p>

  • 10.

    name a flower<br>
    <input type=”radio” name=”q10″ value=”rose”>rose<br>
    <input type=”radio” name=”q10″ value=”key”>key<br>
    <input type=”radio” name=”q10″ value=”stick”>stick<br>
    <input type=”radio” name=”q10″ value=”black”>black<br>
    <p>

  • <input type=”button” value=”Get score” onClick=”getScore(this.form)”>
    <input type=”reset” value=”Clear”><p>
    Score = <input type=text size=15 name=”percentage”><br>
    Correct answers:<br>
    <textarea name=”solutions” wrap=”virtual” rows=”4″ cols=”40″></textarea>
    </form>

    </body></html>


    ———–

    thank you

    to post a comment
    JavaScript

    13 Comments(s)

    Copy linkTweet thisAlerts:
    @JMRKERJul 24.2017 — Welcome to the forums.

    Why did you not start with the code in the referenced link?

    It does what I believe you are asking, while your 1st post here does none of the suggestions in the link referenced.

    Is it that you don't know how to change the questions in the example of the other thread to your question/responses?

    What are your questions related to the other link you wish to ask?

    ?

    From: http://www.webdeveloper.com/forum/showthread.php?286477-How-to-make-a-javascript-quiz-to-choose-10-random-questions-from-100-questions post #13

    <i>
    </i>&lt;!DOCTYPE html&gt;

    &lt;html lang="en"&gt;
    &lt;head&gt;
    &lt;meta charset="UTF-8" /&gt;

    &lt;title&gt; Untitled &lt;/title&gt;
    &lt;style type="text/css"&gt;
    .qno { display: inline-block; width: 100px; }
    .resp { display: inline-block; width: 150px; }
    &lt;/style&gt;

    &lt;script type="text/javascript"&gt;
    // For:
    // http://www.webdeveloper.com/forum/showthread.php?286477
    // -How-to-make-a-javascript-quiz-to-choose-10-random-questions-from-100-questions&amp;p=1296827#post1296827

    var QuesResp = [
    ['Dog','Cat'], ['King','Queen'], ['TV','Radio'], ['Flashlight','Candle'], ['Book','Tablet'],
    ['Desk','Chair'], ['Guitar','Banjo'],['Jester','Clown'], ['Coffee','Tea'], ['Coat','Pants'],
    ['Clock','Watch'], ['Rain','Snow'], ['Rats','Mice'], ['Tie','Scarf'], ['Hat','Cap'],
    ['Loafers','Wingtips'], ['Phamphlet','Magazine'], ['Cup','Pint'], ['Gallon','Liter'], ['Keyboard','Stylus'],
    ['Chair','Table'], ['Pen','Pencil'], ['Box','Circle'], ['Triangle','Rectangle'],['Gun','Knife'] // no comma after last entry
    ];
    &lt;/script&gt;

    &lt;/head&gt;
    &lt;body&gt;
    &lt;div id="QuestionsResponses"&gt;&lt;/div&gt;

    &lt;script type="text/javascript"&gt;
    var maxQues = 5;
    var QRpointers = [];

    function randOrd() { return (Math.round(Math.random())-0.5); } // quick and dirty randomizer (not the best)

    window.onload = function() {
    for (var i=0; i&lt;QuesResp.length; i++) { QRpointers.push(i); }
    QRpointers.sort(randOrd); // better randomizers are available

    var str = '&lt;h1&gt;Enter common associations&lt;/h1&gt;';
    for (var Q=0; Q&lt;maxQues; Q++) {
    str += '&lt;div class="qno"&gt;Question #'+(Q+1)+'&lt;/div&gt; ';
    str += QuesResp[QRpointers[Q]][0];
    // str += ' &lt;input type="text" id="R'+Q+'" value="' +QuesResp[QRpointers[Q]][1]+ '"&gt;';
    str += ' &lt;div class="resp"&gt;&lt;input type="text" id="R'+Q+'"&gt;&lt;/div&gt;&lt;p&gt;';
    }
    str += '&lt;INPUT value="CHECK ALL" type="button" onclick="scoreme()"&gt;';
    str += '&lt;INPUT id="score" type="text" size="8"&gt;';
    document.getElementById('QuestionsResponses').innerHTML = str;
    }
    function scoreme() {
    var sum = 0; var total=maxQues;
    var corr, resp;
    var sel = document.getElementById('QuestionsResponses').getElementsByTagName('input'); <br/>
    for (var i=0; i&lt;sel.length-1; i++) {
    corr = QuesResp[QRpointers[i]][1];
    resp = sel[i].value;
    if (resp != '') {
    if ( (corr.toLowerCase().indexOf(resp.toLowerCase()) ) != -1) { sum++; // for part match
    sel[i].style.backgroundColor = 'lime'; // set background color (green ???) for correct
    } else {
    sel[i].style.backgroundColor = 'pink'; // highlight incorrect answer in red
    }
    // if (corr.toLowerCase() == resp.toLowerCase()) { sum++; } // for EXACT match
    // if (corr == resp) { sum++; } // for EXACT CASE match
    }
    }
    document.getElementById('score').value = ((sum/maxQues)*100).toFixed(0)+'%';
    }
    &lt;/script&gt;

    &lt;/body&gt;
    &lt;/html&gt;
    Copy linkTweet thisAlerts:
    @yiannis1980authorJul 25.2017 — thanks for your reply!

    i need to check the original post again and see what changes i can make so i will get back to you with questions afterwards...

    but where do i put radio buttons in original post's questions (4 multiple choice questions A/B/C/D)

    i know i lack some basic knowledge and it doesnt feel good bombarding you with simple questions without delving further in html...

    thanks again!
    Copy linkTweet thisAlerts:
    @yiannis1980authorJul 25.2017 — also...i want to put my own questions instead of "question #1"...how do i do that

    thank you
    Copy linkTweet thisAlerts:
    @JMRKERJul 25.2017 — The link you 1st posted was asking questions about 'fill-in-the-blank' style responses.

    If you want to have radio button responses for your quiz, I would suggest the following.

    The QRA array would be posted as an external file to separate the HTML from the answers.


    See commented out <script src="file.js"> part before the QRA definition.

    This example file has only 5 questions, but can be easily expanded upon following the template. You can easily select a randomized sub-set of fewer questions, but given only 5 here will not really show you the potential.

    See reposting below.
    Copy linkTweet thisAlerts:
    @JMRKERJul 25.2017 — Having problems editing and posting complete script above. Trying again...
    <i>
    </i>&lt;!DOCTYPE html&gt;
    &lt;html lang="en"&gt;
    &lt;head&gt;
    &lt;meta charset="UTF-8" /&gt;
    &lt;title&gt; Quiz Template &lt;/title&gt;
    &lt;style&gt;
    p { font-size:1.2em; font-weight:bold; }
    &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;h1&gt; Quiz &lt;/h1&gt;
    &lt;div id="quiz"&gt;&lt;/div&gt;
    &lt;p&gt;&lt;button onclick="score()"&gt;Score&lt;/button&gt;&lt;/p&gt;

    &lt;!-- script src='quizExample.js'&gt;&lt;/script --&gt;
    &lt;script&gt;
    /* quizExample.js
    Create Objects with Questions and Answers In the QRA array (Question, Responses, Answer)
    can be saved to a JS external file and called by filename
    format: Items [ [ Question, [ Resp0, Resp1, ...n], Answer(0...n), ], ... ];
    */

    var QRA = [
    ["When a patient states that they have a needle phobia, you should", // Item Question
    ["a. have the patient lie down during the procedure.", // Resp0
    "b. let the patient chew on a piece of candy or gum.", // Resp1
    "c. show the patient how small the needle is.", // Resp2
    "d. let the patient handle the equipment prior to the procedure.", // Resp3
    ],
    0, // Answer of responses
    ], // end of Item Question

    ["An individual comes in for an autologous donation. What is this person coming to do?",
    ["a. Have their blood drawn.",
    "b. Donate their own blood for their own use.",
    "c. Have a fasting blood test.",
    "d. Donate blood for a family member.",
    ],
    1,
    ],
    ["Which of the following is not a body fluid?",
    ["a. CSF",
    "b. Interstitial",
    "c. LDH.",
    "d. Synovial",
    ],
    2,
    ],
    ["Which of the following situations would most likely trigger hematoma formation?",
    ["a. Removing the tourniquet before removing the needle.",
    "b. Filling the evacuated tube only half-full.",
    "c. Removing the needle while the tourniquet is still on the arm.",
    "d. Applying too much pressure on a puncture site.",
    ],
    2,
    ],
    ["When palpating, a vein having which of the following characteristics
    would be the most appropriate to select for venipuncture?",
    ["a. Hard",
    "b. Bouncy",
    "c. Thin",
    "d. Pulsating",
    ],
    1,
    ],
    ];

    &lt;/script&gt;

    &lt;!--
    Original from:
    http://www.webdeveloper.com/forum/showthread.php?354667-Creating-a-quiz-using-arrays-and-radio-buttons&amp;p=1467285#post1467285
    Modified for:
    http://www.codingforums.com/javascript-programming/384549-revamping-js-multiple-choice-quiz.html
    --&gt;
    &lt;script&gt;
    function showItem(ques, item) {
    var str = '&lt;br&gt;&lt;p&gt;Question #'+ques+'&lt;br&gt;';
    str += QRA[item][0]+'n&lt;blockquote&gt;';
    for (var i=0; i&lt;QRA[item][1].length; i++) {
    str += '&lt;label&gt;';
    str += '&lt;input type="radio" name="QRA'+item+'" value="'+(ques-1)+','+i+'"&gt;';
    str += QRA[item][1][i]+'&lt;/label&gt;&lt;br&gt;';
    }
    return '&lt;/blockquote&gt;'+str+'&lt;/p&gt;';
    }

    function score() {
    var responses = [],
    correct = 0,
    total = correctAnswer.length;
    for (var i=0; i&lt;total; i++) { responses.push(getRBtnName('QRA'+i)); }

    for (var i=0; i&lt;total; i++) {
    if (responses[i] == correctAnswer[i]) { correct++; }
    }
    alert(correct+' of '+total+' correctn'+(correct/ correctAnswer.length * 100).toFixed(1)+'%');
    }

    function getRBtnName(GrpName) {
    var sel = document.getElementsByName(GrpName);
    var fnd = -1;
    var str = '';
    for (var i=0; i&lt;sel.length; i++) {
    if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
    }
    return fnd; // return option index of selection
    // comment out next line if option index used in line above <br/>
    // return str;
    }

    function shuffle(o) {
    for(var j, x, i=o.length; i; j=Math.floor(Math.random() * i), x=o[--i], o[i]=o[j], o[j]=x); return o; }

    var correctAnswer = [];
    function initDisplay() {
    qra = shuffle(QRA);
    for (var i=0; i&lt;qra.length; i++) {
    document.getElementById('quiz').innerHTML += showItem((i+1),i);
    correctAnswer.push(qra[i][2]);
    }
    }
    initDisplay();

    &lt;/script&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    Copy linkTweet thisAlerts:
    @yiannis1980authorJul 25.2017 — thanks a lot JMRKER for your help and your time! (this is a fast response community!)

    i'll start building on that template and post any questions later...
    Copy linkTweet thisAlerts:
    @rootJul 26.2017 — IMHO, you would be better off using PHP to making your quiz. You don't reveal the questions and the answers in the rendered output, only the server has access to the data.
    Copy linkTweet thisAlerts:
    @JMRKERJul 26.2017 — IMHO, you would be better off using PHP to making your quiz. You don't reveal the questions and the answers in the rendered output, only the server has access to the data.[/QUOTE]

    Totally agree if quiz is for an actual grade and not just practice sessions for study.

    This is part of what I was trying to put into posts #5 and #6 above when I was having problems editing.
    Copy linkTweet thisAlerts:
    @gordonc412Mar 17.2020 — @

    @JMRKER#1511991 but the code you suggested can pull a certain amount of question from a pool? (i.e. 5 questions out 10)
    Copy linkTweet thisAlerts:
    @JMRKERMar 17.2020 — @gordonc412#1616213

    Is there a question in that post?
    Copy linkTweet thisAlerts:
    @bafanamusicMar 18.2020 — [Url=https://bafanamusic.com] audio download [/url]

    [Url=https://bafanamusic.com] bafanamusic [/url]

    [Url=https://bafanamusic.com] latest mp3 download [/url]

    [Url=https://bafanamusic.com] latest Ghanaian Songs [/url]

    [Url=https://bafanamusic.com] justin bieber vatican mp3 [/url]

    [Url=https://bafanamusic.com] latest south african songs [/url]

    [Url=https://bafanamusic.com] latest hiphop songs [/url]

    [Url=https://bafanamusic.com] latest naija songs [/url]

    [Url=https://bafanamusic.com] mp3 album download [/url]

    [Url=https://bafanamusic.com] audio download [/url]

    [Url=http://beatnaija.com] Mp3 album download [/url]

    [Url=http://beatnaija.com] beatnaija [/url]

    [Url=http://beatnaija.com] Latest Naija Songs [/url]

    [Url=http://beatnaija.com] instrumental [/url]

    [Url=http://beatnaija.com] gospel [/url]

    [Url=http://beatnaija.com] music [/url]

    [Url=http://beatnaija.com] album [/url]

    [Url=http://beatnaija.com] audio download [/url]

    [Url=https://soccapro.com] 2 sure odds daily [/url]

    [Url=https://soccapro.com] 3 sure odds daily [/url]

    [Url=https://soccapro.com] 5 sure odds daily [/url]

    [Url=https://soccapro.com] free betting tips [/url]
    Copy linkTweet thisAlerts:
    @gordonc412Mar 18.2020 — @JMRKER#1616272 yea, in regard to your code above, I just dont know how to select and show 5 questions out of 10 questions in the question bank.

    thanks
    Copy linkTweet thisAlerts:
    @JMRKERMar 19.2020 — 5 random questions

    OR

    5 sequential questions starting at a random or selected place in a list of questions?

    Also, do you have some demonstration questions you can put into the format defined in the script?

    There are only 5 questions in the original code, so chosing from 10 is rather difficult :)
    ×

    Success!

    Help @yiannis1980 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 3.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,

    tipper: Anonymous,
    tipped: article
    amount: 10 SATS,
    )...