There is a code that generates a mathematical task and if you give the correct answer, then there will be the next task, how to make a counter that counts how many tasks have already been decided? I'll leave the code here, and also this code on JsFiddle

<div id="problem"></div> <br><br> <input type="text" placeholder="answer" id="answer"></input> <script> function single_problem() { var round = 0; var operator = "+"; if (Math.random() < .6) { operator = "&#8722;"; } var a = Math.floor(Math.random()*10 + 10); var b = Math.floor(Math.random()*(a-1) + 1); var c = a - b; if (operator == "+") { var top = b; var bottom = c; var result = b + c; } else { var top = a; var bottom = b; var result = a - b; } document.getElementById('problem').innerHTML = top + ' ' + operator + ' ' + bottom + '<br><br>' + result; $("#answer").keyup(function(event){ if(event.keyCode == 13){ event.preventDefault(); var answer = document.getElementById('answer').value; console.log(answer); console.log(result); if(answer == result) { console.log('awesome!'); single_problem(); document.getElementById('answer').value = ''; var game = round + 1; console.log(game); } } }); } single_problem(); </script> 

    1 answer 1

    Several options

    1) Global variable - just pull out the var round = 0; outside the function and simply increase it (the definition inside the function should be deleted, that is, what costs 2 lines).

    2) You can simply add a parameter to your single_problem(round) function, which would single_problem(0) for the first time on line 40 of jsfidle code. And with the correct answer (line 30), call this method again, but with the parameter round++ i.e. single_problem(round++) ; This option is more "correct", since you decided to do the task recursively.