Pressing instrument sound button a sound is played. I've tried to match each array element on console.log but is not working.
Why does "console.log" match to array and sometimes not? I mean, console.log sometimes shows the correspondent instrument name to the sound heard, sometimes the wrong name is shown. The first "console.log" is the only one which matches to the sound played.
let index;
let sounds = ["DB.WAV","CLA.WAV","TBN.WAV","REC.WAV","TRI.WAV"];
oldSounds = [];
let playSounds = function () {
index = Math.floor(Math.random() * sounds.length);
thisSound = sounds[index];
oldSounds.push(thisSound);
sounds.splice(index, 1);
if (sounds.length < 1) {
sounds = oldSounds.splice(0, oldSounds.length);
}
$("#element").html("<audio autoplay><source src=\"" + thisSound + "\" type=\"audio/mpeg\"><embed src=\"" + thisSound + "\" hidden=\"true\" autostart=\"true\" /></audio>");
if(index === 0) {
console.log(`Double Bass`);
} else if(index === 1) {
console.log(`Clarinet`);
}else if(index === 2) {
console.log(`Trombone`);
}else if(index === 3) {
console.log(`Recorder`);
}else if(index === 4) {
console.log(`Triangle`);}
}
</script>
<div class="wrapper clearfix">
<div class="player-panel">
<button class="btn-answer"><i class="ion-ios-download-outline"></i>Answer</button>
<div onclick="playSounds()">
<button class="btn-play"><i class="far fa-play-circle"></i>Instrument Sound</button>
</div>
<input type="text" placeholder="Instrument name" class="instrument">
<div id="element"></div>
</div>
</div>