Skip to content
Open
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions submissions/okkkko/memory_pairs_game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Memory Pairs Game</title>
<link rel="stylesheet" href="style.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link
href="https://fonts.googleapis.com/css2?family=Inconsolata&display=swap"
rel="stylesheet"
>
</head>
<body>
<h1 class="game_title">Memory Pairs Game</h1>
<div class="game_field"></div>
<script src="script.js"></script>
</body>
</html>
81 changes: 81 additions & 0 deletions submissions/okkkko/memory_pairs_game/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const cardsId = ["01", "02", "03", "04", "05", "06"];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ids

const cardsArr = [...cardsId, ...cardsId];
const maxNumOfHiddenCards = 12;
const maxNumOfFlippedCards = 2;
const delayForHidden = 100;
const delayForClicked = 800;
const delayForAlert = 700;
let gameField = document.querySelector(".game_field");
let numOfHiddenCards = 0;
let arrOfFlippedCardsId = [];
let firstFlippedContainer;
let secondFlippedContainer;

function createCard(id) {
let flipContainer = document.createElement("div");
flipContainer.className = "flip-container";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

classList api is more flexible for styling

flipContainer.setAttribute("id", id);
gameField.append(flipContainer);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

append after all manipulations

flipContainer.innerHTML = `
<div class="flipper">
<div class="front">
<img src="img/paw.jpg">
</div>
<div class="back">
<img src="img/funny-cat_${id}.jpg">
</div>
</div>`;
}
function createField() {
cardsArr.sort(function () {
return 0.5 - Math.random();
});
cardsArr.forEach((id) => createCard(id));
}
function flipCard(event) {
if (arrOfFlippedCardsId.length == 0) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (arrOfFlippedCardsId.length == 0) {
if (!arrOfFlippedCardsId.length) {

firstFlippedContainer = event.target.closest(".flip-container");
firstFlippedContainer.classList.add("clicked");
arrOfFlippedCardsId.push(firstFlippedContainer.id);
} else if (arrOfFlippedCardsId.length == 1 && event.target.closest('.flip-container')!==firstFlippedContainer) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use strict equality operator

secondFlippedContainer = event.target.closest(".flip-container");
secondFlippedContainer.classList.add("clicked");
arrOfFlippedCardsId.push(secondFlippedContainer.id);
CheckPairs();
}
}
function CheckPairs() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not using camelCase here ?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

probably it is a typo:)

if (
arrOfFlippedCardsId[0] == arrOfFlippedCardsId[1] &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

use destructuring
example
const [firstId, secondId] =arrOfFlippedCardsId

firstFlippedContainer !== secondFlippedContainer
) {
setTimeout(() => {
firstFlippedContainer.classList.add("hidden");
secondFlippedContainer.classList.add("hidden");
arrOfFlippedCardsId = [];
numOfHiddenCards += 2;
setTimeout(() => {
checkNumOfHiddenCard();
}, delayForAlert);
}, delayForHidden);
} else {
setTimeout(() => {
firstFlippedContainer.classList.remove("clicked");
secondFlippedContainer.classList.remove("clicked");
arrOfFlippedCardsId = [];
}, delayForClicked);

}
}
function checkNumOfHiddenCard() {
if (numOfHiddenCards === maxNumOfHiddenCards) {
alert("You win!");

gameField.innerHTML = "";
numOfHiddenCards = 0;
createField();
}
}

createField();
gameField.addEventListener("click", flipCard);
83 changes: 83 additions & 0 deletions submissions/okkkko/memory_pairs_game/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-color: rgb(223, 223, 223);
}
.game_title {
text-align: center;
margin-top: 5vh;
font-size: 32px;
font-family: "Inconsolata", monospace;
font-weight: 500;
}
.game_field {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px 10px;
margin-left: auto;
margin-right: auto;
margin-top: 30px;
width: 630px;
}
.flip-container {
perspective: 1000px;
}

.flip-container.clicked .flipper {
transform: rotateY(180deg);
}
.flip-container.clicked.hidden {
visibility: hidden;
}
.front img,
.back img {
cursor: pointer;
width: 150px;
height: 150px;
border: 2px solid rgb(71, 68, 68);
border-radius:5px;
}
.flip-container {
width: 150px;
height: 150px;
}

.flipper {
transition: 0.6s;
transform-style: preserve-3d;
position: relative;
}

.front,
.back {
backface-visibility: hidden;
position: absolute;
top: 0;
left: 0;
}
.front {
z-index: 2;
transform: rotateY(0deg);
}
.back {
transform: rotateY(180deg);
}
@media (max-width: 660px) {
.game_field {
grid-template-columns: repeat(3, 1fr);
width: 320px;
gap: 5px 5px;
}
.front img,
.back img {
width: 103px;
height: 103px;
}
.flip-container {
width: 103px;
height: 103px;
}
}