I have a function that displays a random word from an array, in a non-repeated way.
I would also like to play a sound clip with each word (that sound would be the word's pronounciation).
I know how to play single sounds, and how to play random sounds from an array of sounds. But if I create an array of sounds, how can I play each one only when the corresponding word is displayed?
This is what I am working with:
const p = document.getElementById("randomWord");
const origWords = ["alpha", "bravo", "charlie", "delta", "echo"];
const audioClips = ["alpha.mp3", "bravo.mp3", "charlie.mp3", "delta.mp3", "echo.mp3"];
let remainingWords = [];
function randomize() {
if (remainingWords.length === 0) remainingWords = origWords.slice();
const {
length
} = remainingWords;
const [word] = remainingWords.splice(Math.floor(Math.random() * length), 1);
p.textContent = word;
}
<button onclick="randomize()" type="button">Random Word</button>
<p id="randomWord"></p>
Aucun commentaire:
Enregistrer un commentaire