Javascript随机测试,但超过1个问题
我编写了这段代码,每次刷新或单击页面时都会显示一个随机问题。但是,我希望它不仅显示一个随机问题,而且每次显示3个不同的随机问题。我该怎么做 谢谢Javascript随机测试,但超过1个问题,javascript,random,Javascript,Random,我编写了这段代码,每次刷新或单击页面时都会显示一个随机问题。但是,我希望它不仅显示一个随机问题,而且每次显示3个不同的随机问题。我该怎么做 谢谢 <script language="JavaScript"> var question= new Array(5) question[0] = "Question 1" question[1] = "Question 2" question[2] = "Question 3"
<script language="JavaScript">
var question= new Array(5)
question[0] = "Question 1"
question[1] = "Question 2"
question[2] = "Question 3"
question[3] = "Question 4 "
question[4] = "Question 5) "
question[5] = "Question 6) "
var rand_int= Math.floor(Math.random()*5);
document.write(question[rand_int]);
</script>
变量问题=新数组(5)
问题[0]=“问题1”
问题[1]=“问题2”
问题[2]=“问题3”
问题[3]=“问题4”
问题[4]=“问题5”)
问题[5]=“问题6)”
var rand_int=Math.floor(Math.random()*5);
写作(问题[rand_int]);
首先,我会远离文档。写——每次都会覆盖页面。相反,您应该附加到DOM元素
<div id="questionsContainer"></div>
每次从列表中抽取一个问题时,您都需要一个新的随机数。定义要显示的问题数:
var questionsVisible = 3;
以questionsVisible
作为停止点,在问题数组上循环:
for (var i = 0; i < questionsVisible; i++) {
var rand_int = Math.floor(Math.random() * question.length); //use the length of the array, not a magic number
//Create and append
var span = document.createElement("span");
span.text = question[rand_int];
questionDiv.appendChild(span);
//Splice the used question
question.splice(rand_int, 1);
}
for(变量i=0;i
这很有效
<script language="JavaScript">
var question= new Array(5)
question[0] = "Question 1"
question[1] = "Question 2"
question[2] = "Question 3"
question[3] = "Question 4"
question[4] = "Question 5"
question[5] = "Question 6"
var numberOfQuestions = 3;
var questions = [];
for(var i=0;i<numberOfQuestions;i++){
var rand_int= Math.floor(Math.random()*question.length);
while(question[rand_int] == null){
var rand_int= Math.floor(Math.random()*question.length);
}
questions.push(question[rand_int]);
question.splice(rand_int, 1);
}
for (var i = 0; i < questions.length; i++) {
document.write(questions[i] + "<br />");
}
</script>
变量问题=新数组(5)
问题[0]=“问题1”
问题[1]=“问题2”
问题[2]=“问题3”
问题[3]=“问题4”
问题[4]=“问题5”
问题[5]=“问题6”
var numberOfQuestions=3;
var问题=[];
对于(var i=0;iIt,您似乎已经有了执行这一次的代码。为了帮助您自己得出答案,请考虑如何在不同的时间执行相同的逻辑(在您的情况下为3次).如果我使用for循环,但我不太确定如何使用,也不确定我会花一段时间去做。知道你需要做的是一半的战斗,应用它将是另一半。答案如下在下面的帖子中,我强烈建议您尝试理解for
循环语法,而不要盲目地将答案直接复制/粘贴到代码中。除了Anthony的建议之外,还值得注意的是,您可能不希望同一个问题列出两次……运行“随机问题选择器”多次编写代码可能会多次生成同一个问题。为了弥补这一点,您需要跟踪已经生成的问题,而不是再次生成这些问题。
<script language="JavaScript">
var question= new Array(5)
question[0] = "Question 1"
question[1] = "Question 2"
question[2] = "Question 3"
question[3] = "Question 4"
question[4] = "Question 5"
question[5] = "Question 6"
var numberOfQuestions = 3;
var questions = [];
for(var i=0;i<numberOfQuestions;i++){
var rand_int= Math.floor(Math.random()*question.length);
while(question[rand_int] == null){
var rand_int= Math.floor(Math.random()*question.length);
}
questions.push(question[rand_int]);
question.splice(rand_int, 1);
}
for (var i = 0; i < questions.length; i++) {
document.write(questions[i] + "<br />");
}
</script>