What's going wrong with Simon already? [Solved]

https://codepen.io/LaerTrech/pen/GNGaJy

Thank you.

Haven’t gone through your entire code yet but I noticed that once you click the “On” button, it will stay on forever, regardless of whether you press it a second time. My suggestion for that is to modify your “On” button click handler to negate the value of start every time you press it, and once it’s false, it will just return. I found ternary expressions to be really handy in this situation.

I just created a click handler to turn it off if clicked again after it’s on.
I’m not sure if I’m using click event handlers correctly, though, to be honest, as I have a creeping suspicion they’re the reason my game’s not working so far.

You really only need one click handler for the “On” button. For example once you click on the button, you can use the NOT logical operator “!” to switch back and forth between true and false (on = !on), and then you can set up your conditionals checking to see whether on is true or false. Also, try to use strict equals “===” for your comparisons.

1 Like

This kind of piggybacks on what @codefu-chivy said. You could get away with using one click handler for all of your click inputs (see https://www.kirupa.com/html5/handling_events_for_many_elements.htm for an example).

It appears that you are using logic to control what happens each round (e.g. if (round === "02")... I’m not convinced that this is the most efficient way to accomplish what you are trying to accomplish – there are quite a view rounds. What might be a little better is to check for equality between simon and the human by utilizing a loop with the round counter.

It looks like are well on your way to finishing this game.

Good work so far.

1 Like

I know. You’re absolutely right. There’s no excuse for inefficiency, but sometimes I impulsively throw together solutions for one problem in order to quickly execute my thoughts toward some other problem. I’ll be following your advice. Thank you.

Still going. Better, but I still have problems.
https://codepen.io/LaerTrech/pen/GNGaJy?editors=1011

I think I’m seeing the problem but I’m not 100 percent sure. It looks like you are pushing 20 random button elements into the array in just round 1 alone, when you should only be pushing one. You don’t need a for loop, all you really need is that random number to push one button into the array at a time. I believe that’s why it plays out the entire sequence all at once.

I solved it during the night. https://codepen.io/LaerTrech/debug/GNGaJy (Have to work on responsiveness still, so only looks right on laptop.)
My simonplay function was continuing to play out every index of simon_array until I added a conditional at the end to check that if ‘i’ had reached the current round, to suspend it. There were a couple other things that I tweaked, too. I’m aware that, right now, the user must press a number of buttons equal to current round in order for their array to be checked if correct. I may or may not change that.
Thanks for all your help!