Skip to content
Advertisement

Javascript sorting function causes game to quit

I have a game in Javascript. It plays clues with buttons and the user needs to remember which buttons to click after the button is lit up and the clue plays.

I have a variable called pattern that holds an array to represent the different buttons to play in different order. Instead of having this fixed pattern I created a function that sorts the array and then I call it in the startGame function, which initiates the game. However, this time with this sorting function, although I click the correct button that is played and lit up, the game automatically quits and says “you lose” which is what happens when you press the incorrect button.

What do I do? I am guessing it has something to do with the guess function but how do I fix it?

JavaScript

HTML:

JavaScript

Advertisement

Answer

You need to increment the guessCounter every time there’s a successful guess. You cannot reset guessCounter = 0 every time you invoke the playClueSequence() function, rather clear it when you startGame(). Find the comments in the code for better clarification.

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