diff --git a/index.html b/index.html index 9386faaf4..b2233d612 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,7 @@ Superhero Memory Game - +
@@ -18,6 +18,7 @@

Score

- + + diff --git a/src/index.js b/src/index.js index 37f3a672d..16313523e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,5 @@ +import MemoryGame from './memory.js'; // Add this line at the top if using modules + const cards = [ { name: 'aquaman', img: 'aquaman.jpg' }, { name: 'batman', img: 'batman.jpg' }, @@ -44,7 +46,9 @@ window.addEventListener('load', (event) => { // Bind the click event of each element to a function document.querySelectorAll('.card').forEach((card) => { card.addEventListener('click', () => { - // TODO: write some code here + card.classList.toggle('turned'); + const cardName = card.getAttribute('data-card-name'); + memoryGame.flipCard(cardName); console.log(`Card clicked: ${card}`); }); }); diff --git a/src/memory.js b/src/memory.js index f6644827e..c443a2d2b 100644 --- a/src/memory.js +++ b/src/memory.js @@ -1,18 +1,33 @@ class MemoryGame { constructor(cards) { this.cards = cards; - // add the rest of the class properties here + this.pickedCards = []; + this.pairsClicked = 0; + this.pairsGuessed = 0; } shuffleCards() { - // ... write your code here + if(!this.cards) return undefined; + for (let i=this.cards.length-1; i>=1; i--){ + const j =Math.floor(Math.random()*(i+1)); + [this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]] + } } checkIfPair(card1, card2) { - // ... write your code here + this.pairsClicked +=1; + if(card1.name === card2.name) { + this.pairsGuessed += 1; + return true; + } + return false; } checkIfFinished() { - // ... write your code here + if (this.pairsGuessed === this.cards.length/2){ + return true; + } + return false; + } }