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
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Superhero Memory Game</title>
<!-- LINK THE STYLES HERE -->
<link rel="stylesheet" href="/styles/style.css">
</head>
<body>
<div>
Expand All @@ -18,6 +18,7 @@ <h2>Score</h2>
</div>
<div id="memory-board"></div>

<!-- LINK THE JAVASCRIPT FILES HERE (keep in mind that the order in which you link them MATTERS) -->
<script src="/src/memory.js"></script>
<script type="module" src="/src/index.js"></script>
</body>
</html>
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -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' },
Expand Down Expand Up @@ -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}`);
});
});
Expand Down
23 changes: 19 additions & 4 deletions src/memory.js
Original file line number Diff line number Diff line change
@@ -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;

}
}