Javascript帮助,随机测验问题
我最近做了一些代码,但我想知道如何让测验从6个问题中选择3个,而不是直接显示6个。我们将不胜感激。我希望测验随机选择3个问题,如果重新加载,则有机会从总共6个问题中选择另外3个问题Javascript帮助,随机测验问题,javascript,html,Javascript,Html,我最近做了一些代码,但我想知道如何让测验从6个问题中选择3个,而不是直接显示6个。我们将不胜感激。我希望测验随机选择3个问题,如果重新加载,则有机会从总共6个问题中选择另外3个问题 <script language="JavaScript"> var numQues = 6; var numChoi = 3; var answers = new Array(6); answers[0] = "16"; answers[1] = "8"; answers[2] = "The Wa
<script language="JavaScript">
var numQues = 6;
var numChoi = 3;
var answers = new Array(6);
answers[0] = "16";
answers[1] = "8";
answers[2] = "The Walking Dead";
answers[3] = "Batman v Superman";
answers[4] = "Tupac";
answers[5] = "@ATAME2016";
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;
}
</script>
</head>
<body>
<form name="quiz">
1. How many movie trailers are on the movie page?<br>
<input type="checkbox" name="question1" value="10">10<br>
<input type="checkbox" name="question1" value="14">14<br>
<input type="checkbox" name="question1" value="16">16<br>
<p>
2. How many pages are on this website?<br>
<input type="checkbox" name="q2" value="8">8<br>
<input type="checkbox" name="q2" value="6">6<br>
<input type="checkbox" name="q2" value="7">7<br>
<p>
3. What's the most popular show this week?<br>
<input type="checkbox" name="q3" value="The Walking Dead">The Walking Dead<br>
<input type="checkbox" name="q3" value="Mandem on the wall">Mandem on the wall<br>
<input type="checkbox" name="q3" value="Cotchin with Ksara">Cotchin with Ksara<br>
<p>
4. What movie has made the most money this week in the box office?<br>
<input type="checkbox" name="q4" value="Ride along 2">Ride along 2<br>
<input type="checkbox" name="q4" value="Batman v Superman">Batman v Superman<br>
<input type="checkbox" name="q4" value="GasMan">GasMan<br>
<p>
5. Which star in the celebrity page is no longer alive?<br>
<input type="checkbox" name="q5" value="Tupac">Tupac<br>
<input type="checkbox" name="q5" value="50 Cent">50 Cent<br>
<input type="checkbox" name="q5" value="Bradley cooper">Bradley cooper<br>
<p>
6. What is our twitter account name (@)?<br>
<input type="checkbox" name="q6" value="@ATAME">@ATAME<br>
<input type="checkbox" name="q6" value="@ATAME2016">@ATAME2016<br>
<input type="checkbox" name="q6" value="@A2THETAME">@A2THETAME<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>
</div>
var numQues=6;
var numChoi=3;
var answers=新数组(6);
答案[0]=“16”;
答案[1]=“8”;
答案[2]=“行尸走肉”;
答案[3]=“蝙蝠侠对超人”;
答案[4]=“Tupac”;
答案[5]=“@ATAME2016”;
函数getScore(表格){
var得分=0;
var currElt;
变量选择;
对于(i=0;i您需要使用Math.random()
函数。请参阅:
以下是一个常见的例子:
我会尝试这样的方法,使用:
//隐藏页面加载时的所有问题。
$('.questions').hide();
//随机显示3个问题。
对于(var i=0;我将您的与一起丢失:例如段落的文本
标记后面是文本。这是一个很好的提示!
// Hides all questions on page load.
$('.questions').hide();
// Randomly shows 3 questions.
for (var i=0; i<3; i++) {
var questionId = Math.floor((Math.random() * 6) + 1);
$('#question-' + questionId).show();
}
<div id="question-1" class="questions">
1. How many movie trailers are on the movie page?<br>
<input type="checkbox" name="question1" value="10">10<br>
<input type="checkbox" name="question1" value="14">14<br>
<input type="checkbox" name="question1" value="16">16<br>
</div>
<p id="question-1" class="questions">
1. How many movie trailers are on the movie page?<br>
<input type="checkbox" name="question1" value="10">10<br>
<input type="checkbox" name="question1" value="14">14<br>
<input type="checkbox" name="question1" value="16">16<br>
</p>