javascript - 数组的动态数组

标签 javascript arrays

我正在创建一个在线测试页面,想要捕获错误的答案并插入另一个数组。我的代码如下。我究竟做错了什么?提前致谢。

var ar, wqs, wqpos = 0;
var oneq = new Array(5);
function renderQuestion()
{
    test = _("test");
  if(pos >= ar.length)
  {
    _("test_status").innerHTML = "Test Completed";
    pos = 0;
    correct = 0;
    wqpos = 0;
    return false;
   }
question = ar[pos][1];
..... rest of code
<input type='radio' name='choices' value='A'> "+chA+"<br>;
...... rest of code
<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 == ar[pos][5])
{
    correct++;
}
else
{
oneq = [ar[pos][1], ar[pos][2], ar[pos][3], ar[pos][4], ar[pos][5]];

问题出在接下来的两行:

wqs[wqpos] = [oneq[0], oneq[1], oneq[2], oneq[3], oneq[4]];
wqpos++;
}
pos++;
renderQuestion();
}

最佳答案

先生。我不明白你的问题,我应该无法解决它吗? 但是,我读了你的代码,我认为方法 renderQuestion() 有问题。因为:

var ar, wqs, wqpos = 0; // ar is undefined

所以,在方法 renderQuestion() 中检查:

if(pos >= ar.length) // ar.length is error: Uncaught TypeError, because ar is undefined

我认为你应该为变量设置默认值。

谢谢。

关于javascript - 数组的动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31283132/

相关文章:

python - for循环中不同大小数组的函数

c - 两个指针(数组)相减,(C语言)

arrays - 将列值从 1 更改为 0

javascript - 将对象数组转换为基元数组(从对象属性中提取)

arrays - 准备 segue 数组时不包含任何数据(使用 swift)

javascript - Extjs-触发网格中操作列的处理程序

javascript - 为什么它夺走了我的功能并使其不再是一个功能?

javascript - 如何访问从 Blob 创建的 ArrayBuffer 的索引?

javascript - Android 5.1 中函数外部的数组元素未定义

javascript - 如何分离 HTML 元素和 CSS 元素并返回它们?