-2
var numCorrect = 0;
var numWrong = 0;

function checkAnswer(numCorrect,numWrong){

var userAns = document.getElementById('ans').value;

if (userAns == x) {
numCorrect++;
}
else {
numWrong++;
console.log("wrong");
}
return numCorrect, numWrong;

}

How do i use pass by value here to increment this number by one instead of the variable reseting itself to its initial value after each run? I initialized to both to zero but it adds 1 to either value and resets to 0

  • Calling `incrementFoo` does *not* reset `foo` to its initial value. Either provide a better description of your problem or an example that actually demonstrates your problem. As it is, I can't make sense of it. – Felix Kling May 25 '17 at 17:16

1 Answers1

0

Primitives are always passed by value. Workaround would be to wrap it in an object.

var foo =  { value: 0 }
function increment(wrapper) {
  wrapper.value++
}
increment(foo)
// foo.value here is incremented
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89