Skip to content
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

Match Avoid #1

Open
wants to merge 2 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
183 changes: 82 additions & 101 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,89 @@
document.addEventListener('DOMContentLoaded', () => {
//card options
const cardArray = [
{
name: 'fries',
img: 'images/fries.png'
},
{
name: 'cheeseburger',
img: 'images/cheeseburger.png'
},
{
name: 'ice-cream',
img: 'images/ice-cream.png'
},
{
name: 'pizza',
img: 'images/pizza.png'
},
{
name: 'milkshake',
img: 'images/milkshake.png'
},
{
name: 'hotdog',
img: 'images/hotdog.png'
},
{
name: 'fries',
img: 'images/fries.png'
},
{
name: 'cheeseburger',
img: 'images/cheeseburger.png'
},
{
name: 'ice-cream',
img: 'images/ice-cream.png'
},
{
name: 'pizza',
img: 'images/pizza.png'
},
{
name: 'milkshake',
img: 'images/milkshake.png'
},
{
name: 'hotdog',
img: 'images/hotdog.png'
}
]

cardArray.sort(() => 0.5 - Math.random())
document.addEventListener("DOMContentLoaded", () => {
//card options
const cardArray = [{
name: "fries",
img: "images/fries.png",
},
{
name: "cheeseburger",
img: "images/cheeseburger.png",
},
{
name: "ice-cream",
img: "images/ice-cream.png",
},
{
name: "pizza",
img: "images/pizza.png",
},
{
name: "milkshake",
img: "images/milkshake.png",
},
{
name: "hotdog",
img: "images/hotdog.png",
},
],
gridCardArray = [...cardArray, ...cardArray].sort(
() => 0.5 - Math.random()
),
grid = document.querySelector(".grid"),
resultDisplay = document.querySelector("#result");
var cardsChosen = [];
var cardsChosenId = [];
const cardsWon = [];

const grid = document.querySelector('.grid')
const resultDisplay = document.querySelector('#result')
var cardsChosen = []
var cardsChosenId = []
const cardsWon = []

//create your board
function createBoard() {
for (let i = 0; i < cardArray.length; i++) {
var card = document.createElement('img')
card.setAttribute('src', 'images/blank.png')
card.setAttribute('data-id', i)
card.addEventListener('click', flipCard)
grid.appendChild(card)
//create your board
function createBoard() {
for (let i = 0; i < gridCardArray.length; i++) {
var card = document.createElement("img");
card.setAttribute("src", "images/blank.png");
card.setAttribute("data-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/white.png')
cards[optionTwoId].setAttribute('src', 'images/white.png')
cardsWon.push(cardsChosen)
} else {
cards[optionOneId].setAttribute('src', 'images/blank.png')
cards[optionTwoId].setAttribute('src', 'images/blank.png')
alert('Sorry, try again')
}
cardsChosen = []
cardsChosenId = []
resultDisplay.textContent = cardsWon.length
if (cardsWon.length === cardArray.length/2) {
resultDisplay.textContent = 'Congratulations! You found them all!'
//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/white.png");
cards[optionTwoId].setAttribute("src", "images/white.png");
cardsWon.push(cardsChosen);
} else {
cards[optionOneId].setAttribute("src", "images/blank.png");
cards[optionTwoId].setAttribute("src", "images/blank.png");
alert("Sorry, try again");
}
cardsChosen = [];
cardsChosenId = [];
resultDisplay.textContent = cardsWon.length;
if (cardsWon.length === gridCardArray.length / 2) {
resultDisplay.textContent = "Congratulations! You found them all!";
}
}
}

//flip your card
function flipCard() {
var cardId = this.getAttribute('data-id')
cardsChosen.push(cardArray[cardId].name)
cardsChosenId.push(cardId)
this.setAttribute('src', cardArray[cardId].img)
if (cardsChosen.length ===2) {
setTimeout(checkForMatch, 500)
//flip your card
function flipCard() {
var cardId = this.getAttribute("data-id");
if (
cardsChosenId.includes(cardId) ||
cardsWon.flat().includes(gridCardArray[cardId].name)
) {
return;
}
cardsChosen.push(gridCardArray[cardId].name);
cardsChosenId.push(cardId);
this.setAttribute("src", gridCardArray[cardId].img);
if (cardsChosen.length === 2) {
setTimeout(checkForMatch, 500);
}
}
}

createBoard()
})
createBoard();
});
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
19 changes: 11 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="UTF-8">
<title>Memory Game</title>
<link rel="stylesheet" href="style.css"></link>
<script src="app.js" charset="utf-8"></script>
<meta charset="UTF-8">
<title>Memory Game</title>
<link rel="stylesheet" href="style.css"></link>
<script src="app.js" charset="utf-8"></script>
</head>

<body>

<h3>Score:<span id="result"></span></h3>
<h3>Score:<span id="result"></span></h3>

<div class="grid">
</div>
<div class="grid">
</div>

</body>
</html>

</html>