DEV Community

Discussion on: Create a memory game with JavaScript

 
fakorededamilola profile image
Fakorede Damilola

Ok I think I have worked on it , the names you will use in each object will be basically the same name has the mp3 like you said.
So what I did was, everytime a card is clicked the name is pushed to an array, I got the name and I appended .mp3 to it and played it.

You can check the link again, but note that mine won't work since I dont have a fries.mp3 :).
And if your music will be in a different folder, you can do this.
source.src=./music/data/${cardsSelected[1]}.mp3
you can use the first or second value since they are basically the same name

Thread Thread
 
olmichalek profile image
olmichalek

Thank you so much. It's really kind of you to work on this problem for me. (it's not so common recently).
I will try your code and see how it works ;) All the best for you and good luck in your programming career!

Thread Thread
 
fakorededamilola profile image
Fakorede Damilola

No problem
I am glad I helped
and thank you

Thread Thread
 
olmichalek profile image
olmichalek

for me is better if I declare sound variable eg. let trousers = new Audio("./trousers.mp3") and later addEvent listener to a picture. Function playSound {trousers.play();}. it words well but how can I let the program know which condition has to be met to play sound. Because if I add event listener to grid or to img he plays sound for all of the pictures in the grid.
But how to choose these specific pictures? We need to label these picture somehow that if they are clicked sound will be played. Can you give me a hint cause I am quite frustrated with this problem and your first solution only play sound if 2 cards are picked either match or wrong. But what I meant is to play sound when every single card is clicked. And sound should be different depending on different card. For trousers => trousers.mp3 for skirt => skirt mp3, etc.

Thread Thread
 
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