I’m a beginner in js, and I’m making that Simon Says game. I’ve made a loop to show the previous sequence, but there’s a problem with the execution. I want it to show the sequence from the beginning, but when there are more colors in the gamePattern array, patClr value is last color and it just repeats the same one. The problem is that the function first does it’s thing, and sets the patClr to the last value in the array, and then does the functions that are in the setTimeout.
this is the code i made:
function showSequence(){
$("h1").text("Level " + level);
wait = 1000;
for (var i = 0; i < gamePattern.length; i++){
var patClr = gamePattern[i];
var patBtn = $("#" + patClr);
setTimeout(function(){
playSound(patClr);
buttonAnimation(patBtn, patClr);
}, wait);
wait = wait + 600;
}
setTimeout(function(){
nextSequence();
}, wait);
}
I’m sure there’s a much better way for this, but I couldn’t find it.