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 #138

Merged
merged 11 commits into from
Aug 15, 2022
Merged

memory-pair-game #138

merged 11 commits into from
Aug 15, 2022

Conversation

Eugene-CG
Copy link
Contributor

Memory Pair Game

Demo
Code base

The code is submitted in a dedicated feature branch.

Only code files are submitted.

Please, review.

Comment on lines 3 to 15
class Card {
constructor(img) {
this.img = img;
}
createCard() {
return `
<li class="card__container">
<div class="card card-back">
<img src="./img/${this.img}">
</div>
</li>`;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

No need for this class here. There is no inheritance, no logic inside, etc. Basically, you're using it for data, so just use objects :)

cards.forEach((card) => (cardsList.innerHTML += card.createCard()));

const flipCard = ({ target }) => {
if (target.closest(".card") || target.closest("img")) {
Copy link
Member

Choose a reason for hiding this comment

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

Why this ||? This two elements has one parent, right?

const flipCard = ({ target }) => {
if (target.closest(".card") || target.closest("img")) {
clickRemember = [...clickRemember, target.firstElementChild];
target.closest(".card").classList.remove("card-back");
Copy link
Member

Choose a reason for hiding this comment

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

Instead of running search in DOM for every card, you can save it into variable inside listener

@@ -0,0 +1,71 @@
const cardsList = document.querySelector(".cards__list");
let clickRemember = [];
Copy link
Member

Choose a reason for hiding this comment

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

Just try to translate this in google translate :) And imagine how it will look like for native speaker :)

https://www.grammarly.com/

const shuffleCards = (cards) => cards.sort(() => Math.random() - 0.5);
const createCardOjects = (cards) => cards.map((imgSrc) => new Card(imgSrc));
const createAllCards = (cards) =>
cards.forEach((card) => (cardsList.innerHTML += card.createCard()));
Copy link
Member

Choose a reason for hiding this comment

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

DOM manipulation in loops.

Adding elements to DOM from a loop is a bad practice. A browser will run reflow and repaint for every element in the loop. Instead, you can:

Use append method, which can add several elements in one operation
Create some wrapper, add your items to the wrapper and then add it to DOM. It will be one operation.
Clone current container. Add items to a container and then replace your old container with a new one. But be aware of event listeners.
Use innerHTML instead

@zonzujiro zonzujiro self-assigned this Aug 13, 2022
@Eugene-CG Eugene-CG requested a review from zonzujiro August 13, 2022 12:57
Comment on lines 43 to 46
cards.forEach((obj) => {
cardListInnerHtml += createCard(obj, i++);
});
cardsList.innerHTML = cardListInnerHtml;
Copy link
Member

Choose a reason for hiding this comment

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

Array#forEach callback has second argument - index

cardsList.innerHTML = cardListInnerHtml;
};
const shuffleCards = (cards) => cards.sort(() => Math.random() - 0.5);
let arr = [];
Copy link
Member

Choose a reason for hiding this comment

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

Common mistake. Variable name

Comment on lines 74 to 75
clicksStorage[0].id.replace(/\d+/g, "") ===
clicksStorage[1].id.replace(/\d+/g, "")
Copy link
Member

Choose a reason for hiding this comment

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

And why you need regexp here? 🤔 I mean, you can just compare ids, without it. No?

Comment on lines 83 to 84
target.closest(".card").classList.add("card-back");
target.closest(".card").classList.remove("hide", "open");
Copy link
Member

Choose a reason for hiding this comment

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

Instead of running search on in the loop, you can run it once and save result into variable

Copy link
Member

Choose a reason for hiding this comment

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

Also, it's same as at line 61-62. So I would move this into separate function - DRY

</li>`;
};
const flipCard = (target) => {
if (target.closest(".card")) {
Copy link
Member

Choose a reason for hiding this comment

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

I would move this into listener so flipCard will just flip cards. Also, it's a good example of unnecessary nested logic - why do you call function for cases in which it will do nothing? Better not call it all.

Comment on lines 41 to 46
let cardListInnerHtml = ``;
let i = 0;
cards.forEach((obj) => {
cardListInnerHtml += createCard(obj, i++);
});
cardsList.innerHTML = cardListInnerHtml;
Copy link
Member

Choose a reason for hiding this comment

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

It will be better to move into separate function. Just for readability

@@ -0,0 +1,92 @@
const cardsList = document.querySelector(".cards__list");
let clicksStorage = [];
Copy link
Member

Choose a reason for hiding this comment

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

const

let clicksStorage = [];

const startGame = () => {
let cards = [
Copy link
Member

Choose a reason for hiding this comment

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

const. Variable is not changed anywhere.

Always use const, except situations in which you will rewrite value in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I cant declare clicksStorage variable with const keyword bcs I change clicksStorage later(69 line)

return `
<li class="card__container">
<div class="card card-back">
<img src="./img/${src}" id="${imgId}${i}">
Copy link
Member

Choose a reason for hiding this comment

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

Instead of concatination, you can use data-img-id=${imgId}. And then compare by them. And there is no requirement that data-* should be unique

@Eugene-CG Eugene-CG requested a review from zonzujiro August 15, 2022 10:58
delete .hide  from style.css
Copy link
Member

@zonzujiro zonzujiro left a comment

Choose a reason for hiding this comment

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

Good job! Done

@zonzujiro zonzujiro merged commit 358f829 into kottans:main Aug 15, 2022
@vovan-zt vovan-zt mentioned this pull request Aug 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants