如何使随机问题出现 [英] How to make random question appear

查看:76
本文介绍了如何使随机问题出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我针对多项选择题的代码,我希望以这样的方式使其在浏览器中只显示4个随机问题中的3个,并为3个问题显示正确的答案.

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.

<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>

推荐答案

首先将所有问题集设置为不显示任何内容,然后根据需要尝试使用此javascript函数,您将获得0到10之间的随机数设置该问题显示块

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

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

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

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







function myFunction() {
    document.getElementById("demo").innerHTML = Math.floor(Math.random() * 6) + 1;
}


<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;
}


(文档).ready( function (){ var 否= 3 ; // 做出的选择数量 // 确保有足够的选择空间 如果(randomquestions.length>否){ // 随机排列数组 随机播放 // 获取前三个 for ( var i = 0 ; i < no; i ++){ // 附加三个div // 考虑使用div模板,例如jQuery模板
(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


这篇关于如何使随机问题出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆