I’m pretty close to being finished with the Simon game, but can’t figure out why all the buttons go off at the same time: https://codepen.io/lalov1/full/MvgvxY/

# Need help with the Simon project

Need help with the Simon project

0

The problem is in the following:

```
for(let i=0; i<=BUTTONS_RANDOM.length; i++){
interval*=i;
console.log('interval = ' + interval); // display interval's value during each iteration
if(BUTTONS_RANDOM[i]===1){
setTimeout(playGreen, interval);
}else if(BUTTONS_RANDOM[i]===2){
setTimeout(playRed, interval);
}else if(BUTTONS_RANDOM[i]===3){
setTimeout(playYellow, interval);
}else if(BUTTONS_RANDOM[i]===4){
setTimeout(playBlue, interval);
}else{
}
}
```

**interval** is equal to **0** during each iteration of the loop, because **i** starts at **0** and during the first iteration of the for loop **interval** gets assigned the value of **interval * i**. Since **1000 * 0 = 0**, all other iterations start with **interval** being equal to 0, so it will always be 0.

I am blurring out a possible solution below in case you can not figure out a work around:

```
// remove the existing let interval = 1000 declaration before the for loop and use the following
for (let i=0; i<=BUTTONS_RANDOM.length; i++){
let interval = (i+1)*1000; // this avoids interval being set to zero iteration after iteration
if(BUTTONS_RANDOM[i]===1){
setTimeout(playGreen, interval);
}else if(BUTTONS_RANDOM[i]===2){
setTimeout(playRed, interval);
}else if(BUTTONS_RANDOM[i]===3){
setTimeout(playYellow, interval);
}else if(BUTTONS_RANDOM[i]===4){
setTimeout(playBlue, interval);
}else{
}
}
```

1 Like

**lalov1**#3

Thanks for your help. Now I’m having trouble with it not turning a different color when it is the same color twice and when the user misses one it doesn’t restart. Any tips?

0 Likes