I have a problem with my math game

Sorry in advance for the code quality. I’m usually harsh at first in order to get the project done, then I start refining everything.

The code that I commented out are not involved with the problem that I am having.

Here is the demo: https://viridian-guitar.gomix.me/

Here is the code: https://gomix.com/#!/project/viridian-guitar

I am in the process of crating a math game that records your answers via keypress (1-9) and will tell your score in the end after 10 problems.

My problem is that I cannot get my program to transition to the next problem correctly. It works perfectly fine the first time when you log an answer. On the second time, the answer to the problem shows up on the console. The problem on the screen doesn’t change along with it. Answering a third time will only re-iterate the console.log with the same answer as the second.

Let me include an explanation involving images to make my question clearer.

Starts off with a random problem and the answer is logged into the console for testing.

When you hit the right answer it goes to the next problem, and logs the answer to that question in the console.

When you hit the right answer again, the 2nd problem stays on the screen and logs the same answer a second time on the console.

After that, when you hit the same answer multiple times, it will re-iterate that second log on the console.

Thanks for any help for solving this problem.


I am at work, so am a bit distracted, but are you sure you change random problem? I have a feeling that you don’t change rng so it always gives same problem again and again.

1 Like

You’re not generating a new random number to get a new problem from your array.

@PortableStick @svmi3195 Thanks guys, Ill take a look into the code again and see what I can do to change the rng. I thought I did, but I guess I did it incorrectly.

The only possible reason I could come up with is that once newProblem = problems[rng]; generates a random number, it will hold that number instead of re-generate every time a number key is pressed.

Is this correct?

Yes and no. That line doesn’t generate a random number at all. rng is initialized with a random number on line 33. You also access the array of problems with a random number on line 59

var randomProblem = problems[Math.floor(Math.random() * problems.length)];

You could just use this line every time you want to access a new problem (though there is a math related bug in this line).