Skip to content
Advertisement

Check if buttons are clicked in certain sequence JS

I would like to make Javascript check if buttons are clicked in certain order (I working on the “Simon Game”). An order is an array of number, and every number corresponds to certain div (button). But something is wrong with my code, and it doesn’t work.

The proper running code do this:

If I click right div-button and if I make a mistake it returns false, if all the sequence is clicked right return true.

JavaScript
JavaScript
JavaScript

Advertisement

Answer

Try to replace === by == in :

JavaScript

Since the checkArr[counter] is a number and this.dataset.sound is a string and triple equals === will compare the type also so the consition will never be reached.

Or you could also parse the string to integer using parseInt() like :

JavaScript

Hope this helps.

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement