I'm creating a Simon game for FreeCodeCamp and I'm trying to use a for loop to play an audio sound for each piece of the pattern.
The code I have now plays the audio all at once which won't work for the game. How can I change this code so that it will play each audio sound individually with a .5 second break in between?
Here's the function that plays the audio and adds visual effect
function playAudio() {
if (colorColor === "red") {
red.play();
$('#red').effect("highlight", { color: colorColor }, 500);
} else if (colorColor === "blue") {
blue.play();
$('#blue').effect("highlight", { color: colorColor }, 500);
} else if (colorColor === "green") {
green.play();
$('#green').effect("highlight", { color: colorColor }, 500);
} else if (colorColor === "yellow") {
yellow.play();
$('#yellow').effect("highlight", { color: colorColor }, 500);
}
}
This is the function where I believe the issue is.
function playPattern() {
for (var i = 0; i < pattern.length; i++) {
colorColor = pattern[i];
setTimeout(playAudio, 500);
}
setTimeout(random, 750);
}
And here's the random() function only because it is called within playPattern()
function random() {
randomColor = colors[Math.floor(Math.random() * colors.length)];
colorColor = randomColor;
colorColor = colorColor.slice(1);
pattern.push(colorColor);
count++;
if (count < 10) {
document.getElementById("count").innerHTML = "0" + count;
} else {
document.getElementById("count").innerHTML = count;
}
playAudio();
pickCount = 0;
userPick = [];
}
Thanks!