Simon, help adding computer's next move after player move

Simon, help adding computer's next move after player move
0.0 0

#1

Hello everyone,

I’m having trouble with the Simon game. So far I’ve gotten the sounds and colors to fire individually for the computer so I’m good there, however I cant seem to figure out a way to add the next move. I’ve got an array for computer moves and player moves. I’m using a for loop to compare the two arrays and if both elements match, I push a random button into the computer array and call my function to play computer moves again. My problem seems to be that because I’m pushing the new computer move inside the for loops I quickly get an infinite loop and crash my game. Anyone have ideas on how I can fix this?

I hope I stated my problem clearly enough. Here’s a link to my codepen.
Simon Game

here is my for loop code in question
function clicker(element) {
document.getElementById(element).onclick = function() {
player.push(element);
console.log(comp);

for(var i = 0; i < comp.length; i++) {
for(var i = 0; i < player.length; i++) {
if (player[i] === comp[i]) {
random();
console.log(comp);
press(0);
comp.forEach(press);
}

and here’s how I’ve individually delayed the color change and sound function
function press(index) {
if (comp.length >= index) {
window.setTimeout(function(){
click(comp[index]);
press(++index);
},1500)
}
}