Это простая карточная игра, которую я пытаюсь построить. В первом операторе if я проверяю, не совпадают ли URL-адреса в массиве. Если они этого не делают, я повторно показываю обратную сторону изображения карты. Однако в разделе else я хочу удалить обе карты с доски, но удаляет только ту, которая была нажата последней, а не первую и последнюю карты. Я хочу удалить два, которые были сравнены, если они имеют одинаковый URL. Спасибо
const card = document.querySelector(".main-board");
var arr = [];
card.addEventListener("click", function(e){
e.target.src = randomOrder(images);
arr.push(e.target.src);
//This if statement is what's giving me trouble
if(arr[0] !== arr[1]) {
setTimeout(function(){
e.target.src = "https://i.pinimg.com/originals/c1/59/b4/c159b4738dae9c9d8d6417228024de8d.jpg";
}, 2000);
} else {
setTimeout(function(){
e.target.style.display = "none";
}, 2000);
}
if(arr.length > 1) {
arr = [];
}
});
function randomOrder(images) {
return images[Math.floor(Math.random()*images.length)];
}
const images = [
"https://images.unsplash.com/photo-1541233349642-6e425fe6190e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
"https://images.wallpaperscraft.com/image/butterflies_mushrooms_forest_130001_1280x1280.jpg",
"https://image.shutterstock.com/image-photo/bright-spring-view-cameo-island-260nw-1048185397.jpg",
"https://i2.wp.com/buzzonearth.com/wp-content/uploads/2018/12/pexels-photo-814499.jpeg?resize=500%2C333&ssl=1",
"https://images.unsplash.com/photo-1542725577-5240e614e436?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80"
]