I'm trying to create 4 different audio objects in JS in a programmatic way. I have seen similar threads to this like in this thread: "How do I create dynamic variable names inside a loop?"
However, the change in variable name capitalises on the looping stage. Inside the thread there are also answers using the this
keyword, though I am unsure how to use that programmatically in a loop.
Attempt:
var buttonColours = ["red", "blue", "green", "yellow"]
var sounds = [];
for (var i=0; i < buttonColours.length; i++) {
sounds[i] = new Audio(`sounds/${buttonColours[i]}.mp3`); // Eg. name of audio file - 'blue.mp3'
}
console.log(sounds);
Current Output:
[audio, audio, audio, audio]
Desired Output (each storing a different Audio object as they play different sounds):
[redSound, blueSound, greenSound, yellowSound]