Skip to content
Merged
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
107 changes: 107 additions & 0 deletions JSMemoryGame/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
document.addEventListener('DOMContentLoaded', () => {
//card options -
const cardArray = [
{
name: 'gal-1',
img: 'images/gal-1.jpeg'
},
{
name: 'gal-2',
img: 'images/gal-2.jpeg'
},
{
name: 'gal-3',
img: 'images/gal-3.jpeg'
},
{
name: 'gal-4',
img: 'images/gal-4.jpeg'
},
{
name: 'gal-5',
img: 'images/gal-5.jpeg'
},
{
name: 'gal-6',
img: 'images/gal-6.jpeg'
},
{
name: 'gal-1',
img: 'images/gal-1.jpeg'
},
{
name: 'gal-2',
img: 'images/gal-2.jpeg'
},
{
name: 'gal-3',
img: 'images/gal-3.jpeg'
},
{
name: 'gal-4',
img: 'images/gal-4.jpeg'
},
{
name: 'gal-5',
img: 'images/gal-5.jpeg'
},
{
name: 'gal-6',
img: 'images/gal-6.jpeg'
}
];

cardArray.sort(() => 0.5 - Math.random());

const grid = document.querySelector('.grid');
const resultDisplay = document.querySelector('#result');
var cardsChosen = [];
var cardsChosenId = [];
var cardsWon = [];
//create your board
function createBoard() {
for (let i = 0; i < cardArray.length; i++) {
var card = document.createElement('img');
card.setAttribute('src', 'images/start.jpeg');
card.setAttribute('date-id', i);
card.addEventListener('click', flipcard);
grid.appendChild(card);
}
}

//check for matches
function checkForMatch() {
var cards = document.querySelectorAll('img');
const optionOneId = cardsChosenId[0];
const optionTwoId = cardsChosenId[1];
if (cardsChosen[0] === cardsChosen[1]) {
alert('You found a match');
cards[optionOneId].setAttribute('src', 'images/logo.png');
cards[optionTwoId].setAttribute('src', 'images/logo.png');
cardsWon.push(cardsChosen);
} else {
cards[optionOneId].setAttribute('src', 'images/start.jpeg');
cards[optionTwoId].setAttribute('src', 'images/start.jpeg');
alert('Sorry, try again');
}
cardsChosen = [];
cardsChosenId = [];
resultDisplay.textContent = cardsWon.length;
if (cardsWon.length === cardArray.length / 2) {
resultDisplay.textContent = 'Congratulations! You found the items';
}
}

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

createBoard();
});
Binary file added JSMemoryGame/images/gal-1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JSMemoryGame/images/gal-2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JSMemoryGame/images/gal-3.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JSMemoryGame/images/gal-4.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JSMemoryGame/images/gal-5.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JSMemoryGame/images/gal-6.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JSMemoryGame/images/gal-7.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JSMemoryGame/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JSMemoryGame/images/start.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions JSMemoryGame/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Memory Game</title>
<script src="app.js"></script>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h3>Score: <span id="result"></span></h3>

<div class="grid"></div>
</body>
</html>
6 changes: 6 additions & 0 deletions JSMemoryGame/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.grid {
display: flex;
flex-wrap: wrap;
height: 300px;
width: 400px;
}