期望一个赋值或函数调用,而不是在调用函数时看到一个表达式

Expected an assignment or function call and instead saw an expression when calling function

我正在制作一个 flashcard/test 程序。出于某种原因,我在调用函数 getAns 时没有检查结果。 JSHint 给出了这个错误:

Expected an assignment or function call and instead saw an expression.

这是我的代码:

function getAns() {
  if (answer[i] == lastPress) {
    document.getElementById("ans").innerHTML = "You're correct! The answer was " + lastPress;
  } else {
    document.getElementById("ans").innerHTML = "We're sorry, but that is not correct. The answer was " + answer[i];
  }
  i++;
  document.getElementById("question").innerHTML = questions[i];
}
document.getElementById("true").onclick = function() {
  lastPress = true
};
document.getElementById("true").onclick = function() {
  getAns
};
document.getElementById("false").onclick = function() {
  lastPress = false
};
document.getElementById("false").onclick = function() {
  getAns
};

您似乎在将 getAns 用作语句而不是将其作为函数调用时遇到了错误。

理想情况下,您的 onclick 函数会将最后一次按键传递给 getAns()。例如:

document.getElementById("true").onclick = function() {
   getAns(true);
};

您的处理程序将收到以下值:

function getAns(lastPress) {
    ...
}