Note that there are some explanatory texts on larger screens.

plurals
  1. POrandom quiz with radio buttons javascript
    primarykey
    data
    text
    <p>I want to do the following. When a user visits my page I want to have a prompt asking the user how many questions they want to take (from 1 till 10). If the user enters e.g 5 then 5 randomly selected questions should be displayed (out of 10 possible). Also, there must be no duplicates. I would like to inform you that I am not experienced in javascript at all. I have done something but I need your help about the arrays. I have put a comment where I need help. I am also open for other ways on how to do that. </p> <p>Thanks</p> <p>html code with 3 questions radio buttons as an example</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;title&gt; Submitting information &lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="quiz"&gt; &lt;br/&gt; What's the capital of Bulgaria? &lt;br&gt; &lt;input type="radio" name="q1" id="city1" value="Sofia"/&gt; &lt;label for="city1"&gt;Sofia&lt;/label&gt; &lt;br&gt; &lt;input type="radio" name="q1" id="city2" value="Buchurest" /&gt; &lt;label for="city2"&gt;Buchurest&lt;/label&gt; &lt;br&gt; &lt;input type="radio" name="q1" id="city3" value="Skopie" /&gt; &lt;label for="city3"&gt;Skopie&lt;/label&gt; &lt;br&gt; &lt;br&gt; &lt;br&gt; What's the capital of Mexico? &lt;br&gt; &lt;input type="radio" name="q2" id="cit1" value="Mexico city"/&gt; &lt;label for="cit1"&gt;Mexico city&lt;/label&gt; &lt;br&gt; &lt;input type="radio" name="q2" id="cit2" value="Karakaz" /&gt; &lt;label for="cit2"&gt;Karakaz&lt;/label&gt; &lt;br&gt; &lt;input type="radio" name="q2" id="cit3" value="Lisbon" /&gt; &lt;label for="cit3"&gt;Lisbon&lt;/label&gt; &lt;br&gt; &lt;br&gt; &lt;br&gt; Who's the current UK prime minister? &lt;br&gt; &lt;input type="radio" name="q3" id="qq1" value="David Cameron"/&gt; &lt;label for="qq1"&gt;David Cameron&lt;/label&gt; &lt;br&gt; &lt;input type="radio" name="q3" id="qq2" value="Lloyd George" /&gt; &lt;label for="qq2"&gt;Lloyd George&lt;/label&gt; &lt;br&gt; &lt;input type="radio" name="q3" id="qq3" value="Gordon Brown" /&gt; &lt;label for="qq3"&gt;Gordon Brown&lt;/label&gt; &lt;/p&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And here is a tricky part. That's my javascript code:</p> <pre><code> function randomInt(low, high) // Given : low &lt;= high // Returns : a random integer in the range [low, high] { return Math.floor(Math.random()*(high-low+1)) + low; } function randomOneOf(list) // Given : list is a nonempty list (array) // Returns: a random item from the list { return list[randomInt(0, list.length-1)]; } var global = prompt("Select No of questions which you want to be displayed") // function to document.write specific No of random questions from a set of questions (between 1 and 10). // For example user may prompt 5 random questions to be displayed out of 10 function result() { // create an array to store the quiz questions var arr = new Array(); arr[0] = ?? //how to store the quiz questions??. Maybe form('quiz').q1 or ??. Thanks arr[1] = ?? arr[2] = ?? .. // till 10 // create another array to store the random number questions that should appear var newarr = new Array(); // finding the first random no. var s = function randomOneOf(arr) // add the first number to the new array of random numbers newarr.push(s); n = 1; // add the rest of the questions to the array depending on global var (how many numbers of questions the user wants) while (n &lt;= global) { // find the 2nd random Number n = function randomOneOf(arr) for (var i = 0; i &lt; newarr.length; i++) { // not allowing duplicates within the random numbers if n.value != newarr[i] { newarr.push(n) n++ } } } // displaying the random questions for (var k = 0; k &lt; newarr.length; k++) { for j = 0; j &lt; arr.length; j++) if (newarr[k] == arr[j]) { document.write(newarr[k]); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload