-1

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
};
mplungjan
  • 169,008
  • 28
  • 173
  • 236
ARI FISHER
  • 343
  • 1
  • 13
  • 3
    `getAns` is an expression, not an assignment (`getAns = ...`) or function call (`getAns()`). Also you seem to be adding two different `onclick` functions to each element. – jonrsharpe Dec 09 '19 at 16:57
  • 1
    Re @jonrsharpe's second point: If you want to do two things in your handler, write one after another in a single handler function: `.onclick = function() { lastPress = true; getAns(); };` But also look at using modern event handling (`addEventListener`), rather than assigning to `onclick`. – T.J. Crowder Dec 09 '19 at 16:59
  • Also `document.getElementById("true").onclick = function() { lastPress = true; getAns(); };` – mplungjan Dec 09 '19 at 16:59
  • Could you paste the code with the line numbers? Also pasting the exact error (with line number) would help – paroxyzm Dec 09 '19 at 17:55

1 Answers1

0

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) {
    ...
}
Ed Lucas
  • 5,955
  • 4
  • 30
  • 42