Uncaught TypeError: Cannot read property 'choices' of undefined

Uncaught TypeError: Cannot read property 'choices' of undefined

我正在努力提高我的 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)

如何解决?

提前谢谢你。

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

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

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