Search code examples
javascripthtmljshint

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


I am making a flashcard/test program. For some reason, I'm not checking the result when I call the function getAns. JSHint is giving this error:

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

Here's my code:

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
};

Solution

  • It looks like you're getting the error from using getAns as a statement and not calling it as a function.

    Ideally, your onclick functions would pass the last keypress to getAns(). For example:

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

    And your handler would receive the value:

    function getAns(lastPress) {
        ...
    }