DEV Community

Discussion on: Create a memory game with JavaScript

 
fakorededamilola profile image
Fakorede Damilola • Edited

Good day,
like I said earlier, I am not really familiar with the audio api, so I cannot really tweak your method. So this is what I came up with.

Since everytime a card is clicked, we get the dataset-id and then use this to get the name from the array, you can easily play the sound from there
let clicked =cardArray[selected].name
cardsSelected.push(clicked);

source.src=${clicked}.mp3
audio.load()
audio.play()
I also updated it in the link.

But for your method, you can try
let clicked =cardArray[selected].name
cardsSelected.push(clicked);
let sound =new Audio(./${clicked}.mp3)
function playSound(sound){
sound.play()
}
playSound(sound).

This will be called each time a card is clicked, you can just simply interchange it with the one above. I commented this out in the codepen link.

Thread Thread
 
olmichalek profile image
olmichalek

hello bro ;) I did it
check my game out here:
english-quiz.cba.pl/Memory%20game/...
thanks for all your help

Thread Thread
 
fakorededamilola profile image
Fakorede Damilola

Whoa, I love this.
Well done :) :)

Thread Thread
 
rahatjabeen1 profile image
Rahatjabeen1

hello sir can you please help me i added audio in my game but 'm facing some issue with the audio