Simon Game Help!

Simon Game Help!


I need help with making Simon Game. I made a function to check if the computer raandom clicks are the same as the player clicks. But every time when I click start it displays ‘try again’. I don’t know what to change to make this function check every time the user clicks.

Please help! Here is my code:


Well you call newRound function in startGame function, and newRound calls for checkClicks which is probably why you get a ‘try again’ alert when you click start.


When you start the game, your code calls the newRound function which calls computer function and then checkClicks function. The checkClicks function has the following if statement:

 if(userClicks[userClicks.length - 1] == seq[seq.length - 1]){

The above evaluates to false, so the else block of code (below) executes. Why does it evaluate to false? Because at this point in the game, userClicks[userClicks.length -1] is undefined and seq[seq.length -1] is 1. undefined is not equal to 1, so this evaluates to false. Why is userClicks[userClicks.length -1] undefined? Because userClicks.length is 0 (zero), so subtracting 1 from it becomes -1 and there is no -1 index of an array, so you get undefined.

    alert("Try again!");

That is why you get the Try Again alert after clicking the start button.


Thanks, but what should I do to make this function run every time the user clicks on a button to compare it to the seq array, or should I remove that function when the user clicks Start?


You need to make sure userClicks has values in it before you try to make the comparison.


But when I click on a button the usrClick() function pushes values to the userClicks array


Unfortunately, I believe you are going to have re-think the logic of a couple of your functions.


Ok, thanks for the help! :slight_smile:


I’m making a Simon game and I’ve already added the logic but I don’t know how to pause an execution untill the player has pressed on a tile. I’ve made a function called user() that records users input and pushes it to the userSeq array. Then I made a function checkTurn() to check who’s turn is next (the computer or the user). If it’s the user’s turn then I want in the else to wait for the user() function to finish and then call the callcheckClicks() to check if user’s input is the same as computer’s. Please help! :slight_smile:

Here’s a link: