Модифицированный код немного изменил положение вещей в основном и добавил setTimeout, чтобы пользователь мог видеть цвет до того, как он станет черным или белым, также добавил event.target в массив picks, чтобы вы знали, с какими элементами работать:
var elements = document.getElementsByClassName('grid-cell');
var picks = [];
//Event Listeners to respond to a click on the squares.
for (var i = 0; i < elements.length; ++i) {
elements[i].addEventListener('click', showColor);
}
function showColor(event){
this.style.backgroundColor = this.getAttribute('data-color');
picks.push({
color: this.getAttribute('data-color'),
target: event.target
});
if(picks.length === 2){
checkMatch();
}
}
function checkMatch(){
if(picks[0].color === picks[1].color){
console.log("Match");
setTimeout(function(){
picks.forEach(function(element){
element.target.style.backgroundColor = 'white';
picks = [];
})
},500);
}else{
console.log("Not a match");
setTimeout(function(){
picks.forEach(function(element){
element.target.style.backgroundColor = 'black';
});
picks = [];
},500);
}
}
Это можно немного изменить и улучшить некоторые функции, но вы получите общее представление.