I'm currently working on a Pairs game in JavaScript but I don't know how to use the picture only two times not more.
I've made an Array of objects with the pictures that have as key ID, src and a name. I loop through this array to have the id and use it in the template in a random way. So I don't have the same picture next to each other.
This is the current state of my code :
for (let index = 0 ; index < 18; index++) {
let x = Math.round(Math.random() * 8);
let template = document.getElementsByTagName('template')[0];
let target = document.getElementById('Playground');
let clone = template.content.cloneNode(true);
clone.querySelector("img").src= item[x].src;
clone.querySelector("img").alt = item[x].id;
clone.querySelector('button').setAttribute('id', i++);
clone.querySelector('button').setAttribute('Onclick', null);
target.appendChild(clone);
};
I think the issue is the x variable that generate randomly a picture in the template. But the same picture return more than or less than 2 time or some pictures aren't used.
Can someone help me please ?