Skip to content

[RMT-WD-PT-01-2024] , Nawel Chafi, Abraham Agusti, Ama Williams #1945

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<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 @@ -16,8 +17,14 @@ <h2>Score</h2>
<p>Pairs clicked: <span id="pairs-clicked">0</span></p>
<p>Pairs guessed: <span id="pairs-guessed">0</span></p>
</div>
<button>click Here</button>
<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 src="./src/index.js"></script>


</body>
</html>
77 changes: 75 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@



const cards = [
{ name: 'aquaman', img: 'aquaman.jpg' },
{ name: 'batman', img: 'batman.jpg' },
Expand Down Expand Up @@ -27,7 +30,9 @@ const cards = [

const memoryGame = new MemoryGame(cards);

window.addEventListener('load', (event) => {
let isProcessing= false;

window.addEventListener("load", (event) => {
let html = '';
memoryGame.cards.forEach((pic) => {
html += `
Expand All @@ -41,11 +46,79 @@ window.addEventListener('load', (event) => {
// Add all the divs to the HTML
document.querySelector('#memory-board').innerHTML = html;

const pairsClicked=document.querySelector("#pairs-clicked")
const pairsGuessed=document.querySelector("#pairs-guessed")

// Bind the click event of each element to a function
document.querySelectorAll('.card').forEach((card) => {
card.addEventListener('click', () => {
// TODO: write some code here
console.log(`Card clicked: ${card}`);
if (isProcessing){
return;
}
//we turn the card and push it into the empty pickedCards array
card.classList.add("turned")
memoryGame.pickedCards.push(card)

//Adds 1 to the pairsClicked and adds it in the HTML
memoryGame.pairsClicked++;
pairsClicked.innerHTML=memoryGame.pairsClicked;

//to check if the person has clicked 2 cards
if (memoryGame.pickedCards.length===2){
isProcessing=true;

const check =memoryGame.checkIfPair(
memoryGame.pickedCards[0].getAttribute("data-card-name"),
memoryGame.pickedCards[1].getAttribute("data-card-name")
);

if (check){
const finish=memoryGame.checkIfFinished();
if (finish){
window.alert("Woooo-hoooooo!!!!! You won!👏👏👏👏👏👏👏👏");
setTimeout(()=>{
memoryGame.pairsClicked=0;
pairsClicked.innerHTML=memoryGame.pairsClicked;

memoryGame.pairsGuessed=0;
pairsGuessed.innerHTML=memoryGame.pairsGuessed;

document.querySelectorAll(".card").forEach((card) => {
card.classList.remove("turned")
})
},1000)

}

pairsGuessed.innerHTML = memoryGame.pairsGuessed;
memoryGame.pickedCards=[]

isProcessing=false;


}
else {
setTimeout(()=>{
memoryGame.pickedCards[0].classList.remove("turned");
memoryGame.pickedCards[1].classList.remove("turned");
memoryGame.pickedCards= [];

isProcessing=false;
}, 1000);
}
}

//console.log(`Card clicked: ${card}`);
});

});

});







44 changes: 40 additions & 4 deletions src/memory.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
class MemoryGame {
constructor(cards) {
this.cards = cards;
console.log(cards)
// add the rest of the class properties here
this.pickedCards=[];
this.pairsClicked=0;
this.pairsGuessed=0;
}

//Iteration 2.2
shuffleCards() {
// ... write your code here
//should return the shuffled (mixed) array of cards
let shuffle=0;
if (this.cards){
for (let i=0;i<this.cards.length -1;i++){
let rand=Math.floor(Math.random()*(i +1));
shuffle=this.cards[rand];
this.cards[rand]=this.cards[i];
this.cards[i]=shuffle;
}
return this.cards;
}
return undefined
}


checkIfPair(card1, card2) {
// ... write your code here
}
this.pairsClicked++
//if(card1.name===card2.name){
if(card1===card2){
this.pairsGuessed++
return true
}
else {
return false
//}
}
}


checkIfFinished() {
// ... write your code here
if(this.pairsGuessed===0){
return false
}
if(this.pairsGuessed===this.cards.length/2){
return true
}
else {
return false
}
}
}