Skip to content
Open
Show file tree
Hide file tree
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
27 changes: 19 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,30 @@ window.addEventListener('load', (event) => {
document.querySelectorAll('.card').forEach((card) => {
card.addEventListener('click', () => {
// TODO: write some code here
memoryGame.pickedCards.push(card);
if (memoryGame.pickedCards.length === 1) {

if (memoryGame.pickedCards.length < 2) {
memoryGame.pickedCards.push(card);
card.setAttribute('class', 'card turned');
} else if (memoryGame.pickedCards.length === 2) {
let result = memoryGame.checkIfPair(memoryGame.pickedCards[0], memoryGame.pickedCards[1]);
if (result === true) {
}

if (memoryGame.pickedCards.length === 2) {
console.log(memoryGame.pickedCards[0])
let card1 = memoryGame.pickedCards[0].getAttribute("data-card-name")
let card2 = memoryGame.pickedCards[1].getAttribute('data-card-name')

let result = memoryGame.checkIfPair(card1, card2);
console.log(result)
if (result) {
memoryGame.pickedCards[0].setAttribute('class', 'card turned');
memoryGame.pickedCards[1].setAttribute('class', 'card turned');
memoryGame.checkIfFinished()
memoryGame.pickedCards = [];
} else {
memoryGame.pickedCards[0].setAttribute('class', 'card');
memoryGame.pickedCards[1].setAttribute('class', 'card');
//memoryGame.pickedCards = [];
memoryGame.pickedCards[0].classList.remove("turned")
memoryGame.pickedCards[1].classList.remove("turned")
memoryGame.checkIfFinished()
memoryGame.pickedCards = [];

}
}
console.log(`Card clicked: ${card}`);
Expand Down
5 changes: 1 addition & 4 deletions src/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ class MemoryGame {
}

checkIfPair(card1, card2) {
if (card1.name === card2.name) {
if (card1 === card2) {
this.pairsClicked++;
this.pairsGuessed++;

return true;
} else {

this.pairsClicked++;
return false;
}
Expand All @@ -30,6 +28,5 @@ class MemoryGame {
} else {
return false;
}

}
}