简体   繁体   English

同时在数组中添加一个元素,在javascript中给出错误

[英]while adding one more element in array giving error in javascript

i have 15 questions quiz . 我有15个问题测验。 its working for 4 questions, while adding one more question its giving error (not displaying anything). 它为4个问题工作,同时再添加一个问题给出错误(不显示任何内容)。 what to change for adding 15 questions. 添加15个问题需要改变什么。 somewhere array size is fixed wrong. 某处数组大小修复错误。

    <script>
        var pos = 0, test, test_status, question, choice, choices, chA, chB, chC,chD, correct = 0;
var questions = [
    ["q1", "Internet Only Thing", "Inter Online Technology", "Internet of Things", "International Online Techn", "C"],
    ["q2", "Computer", "Object", "Mobile", "Tab", "B"],
    ["q3", "a", "b", "xyz","z", "A"],
    ["q4", "hfjh", "fsafs", "fasf", "faas", "C"]
    ["q5", "fasfa", "fasfa", "fasfd","fas", "A"]


    //   ["...", "...", "...", "...", "B"]
     //  ["q5", "...", "...", "...", "...", "C"]
];
function _(x){
    return document.getElementById(x);
}
function renderQuestion(){
    test = _("test");
    if(pos >= questions.length){
        test.innerHTML = "<h2>You gave  "+correct+" answers out of "+questions.length+" questions </h2>";
        _("test_status").innerHTML = "quiz 1 Completed";
        pos = 0;
        correct = 0;
        return false;
    }
    _("test_status").innerHTML = "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Question " + (pos + 1) + " of " + questions.length;
    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' name='choices' value='A'> "+chA+"<br>";
    test.innerHTML += "<input type='radio' name='choices' value='B'> "+chB+"<br>";
    test.innerHTML += "<input type='radio' name='choices' value='C'> " + chC + "<br>";
    test.innerHTML += "<input type='radio' name='choices' value='D'> " + chD + "<br><br>";
    test.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}
function checkAnswer(){
    choices = document.getElementsByName("choices");
    for(var i=0; i<choices.length; i++){
        if(choices[i].checked){
            choice = choices[i].value;
        }
    }
    if(choice == questions[pos][5]){
        correct++;
    }
    pos++;
    renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
    </script>

hey i reviewed your code 嘿,我检查了你的代码

in your code questions from 1 to 4 have 4 options and 1 answer where as in 5th one you only added 3 options and 1 answer. 在您的代码中,从1到4的问题有4个选项和1个答案,而在第5个问题中,您只添加了3个选项和1个答案。

that might be issue please check. 这可能是问题,请检查。

I think you are missing some commas in your questions array. 我想你在问题数组中缺少一些逗号。 Replace it with this: 替换为:

var questions = [
        ["q1", "Internet Only Thing", "Inter Online Technology", "Internet of Things", "International Online Techn", "C"],
        ["q2", "Computer", "Object", "Mobile", "Tab", "B"],
        ["q3", "a", "b", "xyz","z", "A"],
        ["q4", "hfjh", "fsafs", "fasf", "faas", "C"],
        ["q5", "fasfa", "fasfa", "fasfd", "A"],
        ["...", "...", "...", "...", "B"],
        ["q5", "...", "...", "...", "...", "C"]
    ];

Let me know if that works for you. 如果这对您有用,请告诉我。 Tried it and seems to work. 尝试过,似乎工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM