Javascript 随机测验但询问超过 1 个

标签 javascript random

我编写了这段代码,每次刷新或单击页面时都会显示一个随机问题。然而,我希望它不仅显示一个随机问题,而是每次随机显示 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>

最佳答案

这有效。

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

关于Javascript 随机测验但询问超过 1 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22440399/

相关文章:

python - Python 2.7.3 哈希字符串如何用于为随机数生成器提供种子?

javascript - 如何使 div 在视口(viewport)平移时展开?

javascript - 如果 firebaseRef 设置为新值,是否需要调用 .off() ?

javascript - 所有反斜杠将从本地文件路径中删除

javascript - 获取 javascript 字符串中的字素字符数?

Python - 使用没有重复单词的随机并从列表中选择一个备用单词

ruby - 如何在 Ruby 中随机取一段字符串?

python - 字符串中随机字符的索引错误

python - 如何使 random.choice 包含不选择?

javascript - 任何让输入模式在 Safari 和 IE8 上工作的方法<