JavaScript测验应用程序工作正常,但存在未捕获类型错误

JavaScript测验应用程序工作正常,但存在未捕获类型错误,javascript,Javascript,我的代码运行良好 但我仍然得到一个错误“quick.html:179 uncaughttypeerror:无法读取未定义的属性'answer'” 我不知道为什么 我已经尝试了所有可能的方法。 我对这个很感兴趣。 请帮帮我! ` 让start=document.getElementById('start'); let buttonY=document.createElement('button'); buttonY.innerText='Yes'; buttonY.className='optio

我的代码运行良好 但我仍然得到一个错误“quick.html:179 uncaughttypeerror:无法读取未定义的属性'answer'”

我不知道为什么 我已经尝试了所有可能的方法。 我对这个很感兴趣。 请帮帮我! ` 让start=document.getElementById('start'); let buttonY=document.createElement('button'); buttonY.innerText='Yes'; buttonY.className='options'; 让buttonN=document.createElement('button'); buttonN.innerText='否'; buttonN.className='选项'; 让我们回应; 分数=0; 设i=0; 让displayQuestions=document.getElementById('question'); 让问题回答=[ { 问题:“HTML是一种编程语言吗?”, 回答:‘没有’, }, { 问:“JavaScript是一种脚本语言吗?”, 回答:“是的”, }, ];

start.addEventListener('click',()=>{
next();
});
函数next(){
start.style.display='none';
如果(i<问题答案.长度){
displayQuestions.style.display='block';
displayQuestions.innerHTML=问题答案[i]。问题;
显示问题。追加子项(按钮);
显示问题。追加子项(按钮);
buttonY.addEventListener('click',()=>{
响应=buttonY.innerText;
如果(回答==问题答案[i]。答案){
分数++;
控制台日志(响应);
}否则{
警惕(“回答错误”);
}
响应='';
i++;
next();
});
buttonN.addEventListener('单击',()=>{
响应=buttonN.innerText;
如果(回答==问题答案[i]。答案){
分数++;
控制台日志(响应);
}否则{
警惕(“回答错误”);
}
响应='';
i++;
next();
});
}否则{
检查();
}
}
函数检查(){
displayQuestions.innerText=`Score为${Score}`;
}
`

问题在于您在下一个函数中定义了按钮eventListener。我给你做了一个

  start.addEventListener('click', () => {
    next();
  });

  function next() {
    start.style.display = 'none';

    if (i < questionAnswer.length) {
      displayQuestions.style.display = 'block';
      displayQuestions.innerHTML = questionAnswer[i].question;

      displayQuestions.appendChild(buttonY);
      displayQuestions.appendChild(buttonN);

      buttonY.addEventListener('click', () => {
        response = buttonY.innerText;

        if (response == questionAnswer[i].answer) {
          score++;
          console.log(response);
        } else {
          alert('Wrong Answer');
        }
        response = '';
        i++;
        next();
      });
      buttonN.addEventListener('click', () => {
        response = buttonN.innerText;
        if (response == questionAnswer[i].answer) {
          score++;
          console.log(response);
        } else {
          alert('Wrong Answer');
        }
        response = '';
        i++;
        next();
      });
    } else {
      check();
    }
  }

  function check() {
    displayQuestions.innerText = `Score is ${score}`;
  }
</script>`