随机javascript测验

时间:2015-05-20 21:16:37

标签: javascript arrays html5

我已经使用javascript创建了一个在线测验,但我已经能够随机化问题,我试图消除要显示的重复问题,为此我将数据中的随机值存储在“函数检查”中比较该值以查看它是否已经在数组中,但它不起作用,我该如何解决?请注意,随机值将存储在变量pos中,用于获取问题。

    <!Doctype html>
<html>
<head>
    <style>

    </style>
    <script type="text/javascript">
        var   quest=0,test,test_status,question,choice,choices,chA,chB,chC,chD,correct=0;
       var pos=Math.floor(Math.random()*20)+0;
        var array=[];
       array.push(pos);
            var questions=[
            ["What is 1+1","3","2","5","11","B"],
            ["What is 10+10","12","13","20","100","C"],
            ["What is 3+2","5","32","56","28","A"],
            ["what is 1-1","1","20","4","0","D"],
            ["what is 0-1","1","20","4","-1","D"],
            ["who is loky","thor's brother","the hulk","thanos","none","A"],
            ["what is a whale","mammal","fish","bird","insect","A"],
            ["what is a shark","mammal","fish","bird","insect","A"],
            ["what is a pc","personal computer","pass check","program counter","prime combo","A"],
            ["what is the 3mod2","1","2","3","none","A"],
            ["what is the color of the sky","red","blue","white","green","B"],
            ["who was the first man on the moon","neil armstrong","bruce willis","john travolta","rocco siffredi","A"],
            ["what is the name of this website","Dino era","Dino bots","Dino hunter","Dino crisis","A"],
            ["what is the name of the developper of dino era","Brandon","harold","Kersley","Jane","C"],
            ["who is optimus prime","president of us","leader of autobots","a common man","your dad","B"],
            ["what are the power rangers","bunch of faggots","teletubbies","superheroes","don't know","C"],
            ["who is goku","dbz character","teletubbies","soul eater character","death note character","A"],
            ["how many star wars movies are there","2","3","7","6","D"],
            ["what is the weapon that rebels destroyed in starwars, a new hope","death cannon","death star","death creator","lord of death","B"],
            ["what is the opposite of a jedi","storm troopers","emperor","sith","ti-fighter","C"]
    ];
        function elements(x)
        {
            return document.getElementById(x);
        }
        function render()
        {

            test= elements("test");
            if(quest>=10 )
            {
                test.innerHTML= "<h2>You Got "+correct+" of "+quest+" questions correct</h2>";
                elements("status").innerHTML ="Test Completed";
                return(false);
            }

           elements("status").innerHTML= "Question " +(quest+1);
           question = questions[pos][0];
           chA = questions[pos][1];
            chB = questions[pos][2];
            chC = questions[pos][3];
            chD = questions[pos][4];
            test.innerHTML="<h3>"+question+"</h3>";
            test.innerHTML+="<input type='radio' id='choices1' name='choice' value='A'> "+chA+"<br />";
            test.innerHTML+="<input type='radio' id='choices2' name='choice' value='B'> "+chB+"<br />";
            test.innerHTML+="<input type='radio' id='choices3' name='choice' value='C'> "+chC+"<br />";
            test.innerHTML+="<input type='radio' id='choices4' name='choice' value='D'> "+chD+"<br /><br />";
            test.innerHTML+="<button onclick='check()'>Submit</button>";

        }



        function check()
        {

            choices=document.getElementsByName("choice");

            for(var i=0; i<choices.length; i++)
            {
                if(choices[i].checked)
                {
                    choice=choices[i].value;
                    break;
                }

            }

            if(i==choices.length)
                {
                alert('Select an answer');
                return(false);
                }




            if(choice == questions[pos][5])
            {
                correct++;
            }

            pos=Math.floor(Math.random()*question.length)+0;
            for(i=0; i<array.length; i++)
            {
                if(array[i]!=pos)
                {
                    pos=Math.floor(Math.random()*question.length)+0;
                }

             }

            array.push(pos);
            quest++;
            render();
        }
    window.addEventListener("load",render,false);
    </script>
</head>
<body>
    <h2 id="status"></h2>
    <div id="test"></div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

你可以做那样的事情:

var start = Math.floor(Math.random() * (questions.length - 9));
var i;
for (i = start; i < 10; i++) {
    alert(questions[i]);
}

答案 1 :(得分:0)

您可以尝试这样的事情:

var subset = select(10,questions);

function select(n,qarray) {
    var subset = [];
    for(i=0;i<n;i++) {
        randomIdx = Math.floor(Math.random()*(qarray.length));
        subset[subset.length] = qarray[randomIdx];
        qarray.splice(randomIdx, 1);
    }
    return subset;
}

小提琴演示:http://jsfiddle.net/fjp4y2vu/