Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid reclick on a flipped image + remove click listener on a white image #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ document.addEventListener('DOMContentLoaded', () => {
alert('You found a match')
cards[optionOneId].setAttribute('src', 'images/white.png')
cards[optionTwoId].setAttribute('src', 'images/white.png')

cards[optionOneId].removeEventListener('click', flipCard);
cards[optionTwoId].removeEventListener('click', flipCard);

cardsWon.push(cardsChosen)
} else {
cards[optionOneId].setAttribute('src', 'images/blank.png')
Expand All @@ -88,20 +92,34 @@ document.addEventListener('DOMContentLoaded', () => {
cardsChosen = []
cardsChosenId = []
resultDisplay.textContent = cardsWon.length
if (cardsWon.length === cardArray.length/2) {
if (cardsWon.length === cardArray.length / 2) {
resultDisplay.textContent = 'Congratulations! You found them all!'
}
}


//check for reclick
var oldId = null;
const checkForReclick = (cardId) => {
if(oldId === cardId) return false
else{
oldId = cardId;
return true;
}
}

//flip your card
function flipCard() {
var cardId = this.getAttribute('data-id')
cardsChosen.push(cardArray[cardId].name)
cardsChosenId.push(cardId)
this.setAttribute('src', cardArray[cardId].img)
if (cardsChosen.length ===2) {
setTimeout(checkForMatch, 500)
if (checkForReclick(cardId)) {
cardsChosen.push(cardArray[cardId].name)
cardsChosenId.push(cardId)
this.setAttribute('src', cardArray[cardId].img)
if (cardsChosen.length === 2) {
setTimeout(checkForMatch, 500)
}
}

}

createBoard()
Expand Down