首页 > 解决方案 > 未捕获的类型错误:无法读取未定义的属性“选择”

问题描述

我正在尝试改进我的 JavaScript,我发现了这个:https ://jsbin.com/qacolagedo/edit?html,js,output 。

但在开发人员工具中,我收到此错误:

qacolagedo.js:54 Uncaught TypeError: Cannot read property 'choices' of undefined
    at renderQuestions (qacolagedo.js:54)
    at checkAnswer (qacolagedo.js:72)
    at HTMLInputElement.onclick (runner:1)

这怎么能解决?

先感谢您。

标签: javascriptundefined

解决方案


调用finished函数后停止执行代码。为此,您可以编写return;

else {
    alert("You have a total of " + sumPoints + "/4" + " correct answers!");
    finished();
    return;
}

并且发生错误是因为您尝试choices从不存在的allQuestions数组项中获取。不存在,因为zCount超过allQuestions数组的长度。


推荐阅读