Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is my code for multiple choice question, i want to make it in such a way that only 3 random question out of the 4 appears on the browser and the correct answer shows for the 3 questions.
HTML
<HEAD>

<style type="text/css">
<!--
.bgclr {background-color: white; color: black; font-weight: bold;}
-->
</style>

<script language="JavaScript">

<!-- Begin
// Insert number of questions
var numQues = 5;

// Insert number of choices in each question
var numChoi = 3;

// Insert number of questions displayed in answer area
var answers = new Array(4);

// Insert answers to questions
answers[0] = "Cascading Style Sheets";
answers[1] = "Dynamic HTML";
answers[2] = "Netscape";
answers[3] = "Common Gateway Interface";
answers[4] = "Database Management System";

// Do not change anything below here ...
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] + "\r\n";
  }
  form.solutions.value = correctAnswers;
}
//  End -->
</script>

</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>

<h3>Web Design Quiz</h3>

<form name="quiz">
1. What does CSS stand for?
<ul style="margin-removed 1pt">
  <li><input type="radio" name="q1" value="Colorful Style Symbols">Colorful Style Symbols</li>
  <li><input type="radio" name="q1" value="Cascading Style Sheets">Cascading Style Sheets</li>
  <li><input type="radio" name="q1" value="Computer Style Symbols">Computer Style Symbols</li>
</ul>

2. What does DHTML stand for?
<ul style="margin-removed 1pt">
  <li><input type="radio" name="q2" value="Dramatic HTML">Dramatic HTML</li>
  <li><input type="radio" name="q2" value="Design HTML">Design HTML</li>
  <li><input type="radio" name="q2" value="Dynamic HTML">Dynamic HTML</li>
</ul>
3. Who created Javascript?
<ul style="margin-removed 1pt">
  <li><input type="radio" name="q3" value="Microsoft">Microsoft</li>
  <li><input type="radio" name="q3" value="Netscape">Netscape</li>
  <li><input type="radio" name="q3" value="Sun Micro Systems">Sun Micro Systems</li>
</ul>
4. What does CGI stand for?
<ul style="margin-removed 1pt">
  <li><input type="radio" name="q4" value="Cascading Gate Interaction">Cascading Gate Interaction</li>
  <li><input type="radio" name="q4" value="Common GIF Interface">Common GIF Interface</li>
  <li><input type="radio" name="q4" value="Common Gateway Interface">Common Gateway Interface</li>
</ul>
5. What does DBMS stand for?
<ul style="margin-removed 1pt">
  <li><input type="radio" name="q5" value="Cascading Gate Interaction">Database Management System</li>
  <li><input type="radio" name="q5" value="Common GIF Interface">Common GIF Interface</li>
  <li><input type="radio" name="q5" value="Common Gateway Interface">Common Gateway Interface</li>
</ul>
<input type="button" value="Get score" onClick="getScore(this.form)">
<input type="reset" value="Clear answers">
<p> Score = <input class="bgclr" type="text" size="5" name="percentage" disabled><br><br>
Correct answers:<br>
<textarea class="bgclr" name="solutions" wrap="virtual" rows="5" cols="30" disabled>
</textarea>
</form>
Posted

First of all set all question set to display none than try this javascript funtion as per your need and you'll get random number between 0 to 10 ans set that question display block

HTML
<p>Click the button to display a random number.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>



JavaScript
function myFunction() {
    document.getElementById("demo").innerHTML = Math.floor(Math.random() * 6) + 1;
}
 
Share this answer
 
v3
Comments
yu2ish 24-May-14 3:35am    
i'm not doing php, it's just a static html page, it's for educational purpose, and i still dn't know how to do this :/
Nirav Prabtani 24-May-14 8:46am    
try my updated solution
JavaScript
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

<script>
var randomquestions = [];
randomquestions[0] = "yes or no?";
randomquestions[1] = "green or yellow?";
randomquestions[2] = "hot or cold?";
randomquestions[3] = "satisfied?";
 
function shuffle(array) {
    var currentIndex = array.length,
        temporaryValue, randomIndex;
    // While there remain elements to shuffle...
    while (0 !== currentIndex) {
        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }
    return array;
}
 
$(document).ready(function () {
    var no = 3; // number of choices to make
    //ensure there is enough to choose from
    if (randomquestions.length > no) {
        //shuffle the array
        shuffle(randomquestions);
        //take first three
        for (var i = 0; i < no; i++) {
            //append three divs
            //considre using templates for your divs e.g. jquery templates
            $('#randomquestions').append('<div>' + randomquestions[i] + '</div>');
        }
    }
});
</script>


<div id="randomquestions"></div>


hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900