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

Memory pair game #94

Merged
merged 5 commits into from
Aug 17, 2022
Merged
Changes from 1 commit
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
37 changes: 15 additions & 22 deletions submissions/asaMitaka/Memory pair game/script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
let content = document.querySelector('.content');
const backImg = 'https://i.pinimg.com/originals/13/25/05/132505ba3238e79c00034c905b2ca045.jpg';
let previousClicked = undefined;
let currentClicked = undefined;
let flipped = 0;
let isBoardLocked = false;

let arrOfCards = [
let cardsList = [
{
img: 'https://static3.depositphotos.com/1005348/211/i/450/depositphotos_2114992-stock-photo-aqua-digit-1.jpg',
id: 1
Expand Down Expand Up @@ -36,14 +40,10 @@ let arrOfCards = [
},
];

let shuffledArr = shuffleArr([...arrOfCards, ...arrOfCards]);

shuffledArr.forEach((el) => {
content.innerHTML += renderItem(el);
});
let shuffledCardList = shuffleArr([...cardsList, ...cardsList]).map((el) => content.innerHTML += renderItem(el));
Copy link
Member

@zonzujiro zonzujiro Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still same mistake - you adding elements inside loop. Don't do that :)

You should create string variable in which you will write value, accumulator. And then add it into the DOM in one call.


let cardItems = document.querySelectorAll('.cardItem');
cardItems.forEach(el => el.addEventListener('click', clickedItem));
cardItems.forEach(el => el.addEventListener('click', clickItem));

function shuffleArr(array) {
for (let i = array.length - 1; i > 0; i--) {
Expand All @@ -56,27 +56,20 @@ function shuffleArr(array) {

function renderItem(item) {
return `
<div class='cardItem' data-attribute='${item.id}'>
<div class='cardItem' data-card_id='${item.id}'>
<img class='cardItem__front' src='${backImg}'/>
<img class='cardItem__back' src='${item.img}'/>
</div>
`;
}

let previousClicked = undefined;
let currentClicked = undefined;
let flipped = 0;
let lockBoard = false;

function clickedItem(event) {
if (event.target === previousClicked) return;
if (lockBoard) return;
if (!event.target.classList.contains('cardItem__front')) return;
function clickItem(event) {
if (isBoardLocked || event.target === previousClicked || !event.target.classList.contains('cardItem__front')) return;

let parentElement = event.target.closest('.cardItem');
parentElement.classList.add('flip');

if (previousClicked === undefined) {
if (!previousClicked) {
previousClicked = parentElement;
} else {
currentClicked = parentElement;
Expand All @@ -86,14 +79,14 @@ function clickedItem(event) {
}

function sameItems(previosEvent, currentEvent) {
lockBoard = true;
if (previosEvent.dataset.attribute === currentEvent.dataset.attribute) {
isBoardLocked = true;
if (previosEvent.dataset.card_id === currentEvent.dataset.card_id) {
previosEvent.classList.add('hide');
currentEvent.classList.add('hide');
resetBoardItem();
flipped += 2;

if (flipped === shuffledArr.length) {
if (flipped === shuffledCardList.length) {
alert('YOU WIN!');
window.location.reload();
}
Expand All @@ -109,5 +102,5 @@ function sameItems(previosEvent, currentEvent) {
function resetBoardItem() {
previousClicked = undefined;
currentClicked = undefined;
lockBoard = false;
isBoardLocked = false;
}