-1

everything is working properly only the control is not moving to the if statement

let fnum, snum, operator;
const op = ["+", "-", "*", "/", "%"];
let fnumUi = document.getElementById("fnumUI");
let snumUi = document.getElementById("snumUI");
let opUi = document.getElementById("opUI");
var input = document.getElementById("user_input");
let scoreUi = document.getElementById("score");
input.addEventListener("keypress", myFunction(event));

function myFunction(event) {
  let answer = 8;
  if (event.keyCode === 13) {
    let user_ans = Number(input.value);
    input.value = '';
    fnum = Number(Math.floor(Math.random() * 20));
    snum = Number(Math.floor(Math.random() * 20));
    operator = op[Math.floor(Math.random() * op.length)];
    fnumUi.innerHTML = fnum;
    snumUi.innerHTML = snum;
    opUi.innerHTML = operator;
    if (operator === "+") {
      answer = fnum + snum;
    } else if (operator === "-") {
      answer = fnum - snum;
    } else if (operator === "*") {
      answer = fnum * snum;
    } else if (operator === "/") {
      answer = fnum / snum;
    } else {
      answer = fnum % snum;
    }
    if (answer === user_ans) {
      scoreUi.innerHTML = Number(scoreUi.innerHTML) + 3;
    } else {
      scoreUi.innerHTML = scoreUi.innerHTML - 1;
    }

  }
}

answer is updating but for this part of code

if (answer === user_ans) {
  scoreUi.innerHTML = Number(scoreUi.innerHTML) + 3;
} else {
  scoreUi.innerHTML = scoreUi.innerHTML - 1;
}

even when i write the correct answer it moves to else statement. not getting into if statement so instead of adding 3 in score it is deducting 1 from the score.

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
  • 1
    are the data types same ? did you try `==` instead of `===` ? – mrid Sep 16 '20 at 06:31
  • I tried that than also it is not working – Shruti Singh Sep 16 '20 at 11:07
  • Please try using the [debugging capabilities](https://developer.mozilla.org/en-US/docs/Mozilla/Debugging/Debugging_JavaScript) of your browser. Use the [browser console (dev tools)](https://webmasters.stackexchange.com/q/8525) (hit `F12`) and read any errors. – Sebastian Simon Sep 16 '20 at 18:01
  • `answer === user_ans` is almost always going to be false. Please see [How should I do floating point comparison?](https://stackoverflow.com/q/4915462/4642212). – Sebastian Simon Sep 16 '20 at 18:02

1 Answers1

4

You need to pass the function itself rather than the results of its call.

input.addEventListener("keypress",myFunction);
Vilx-
  • 104,512
  • 87
  • 279
  • 422
  • @ShrutiSingh - No... just change that one line to what I have written above, and it will work. – Vilx- Sep 16 '20 at 11:39
  • @ShrutiSingh - But if you don't understand what I'm saying or what's going on, then tell me that, and I'll write a more complete explanation. – Vilx- Sep 16 '20 at 11:40
  • Ok I will change that part as u said but I was asking if u want to see the whole code that's why I was asking to post the whole code .Thnk u for ur help – Shruti Singh Sep 16 '20 at 14:00
  • I will try and I face any issue I will contact u again – Shruti Singh Sep 16 '20 at 14:00
  • @ShrutiSingh - If this helps, please don't forget to accept the answer with the green checkmark! – Vilx- Sep 16 '20 at 17:05