Hellooo :), very good solution for this challenge, Ilham Bouktir. 🍳
I have a few recommendations that can help you in your future projects:
General:
- Having a solid code structure is key when starting any project, so I recommend you get used to using "file and folder structure patterns." For example:
my-project/
├── build/
│ ├── assets/ # Static files (images, fonts, etc.)
│ │ ├── img/
│ │ ├── fonts/
│ ├── css/
│ │ └── styles.css
│ ├── js/
│ │ ├── main.js
│ └── index.html
│
├── src/
│ ├── assets/
│ │ ├── img/
│ │ └── fonts/
│ ├── scss/
│ │ └── styles.scss
│ ├── js/
│ │ ├── app.js
│ │ ├── components/
│ │ │ └── Header.js
│ │ └── utils.js
├── .gitignore
├── README.md
HTML:
- The use of <div></div> should be for content that is genuinely generic or merely decorative, i.e., when it doesn't carry any weight or meaning in your project.
- Also, it would be appropriate to include the alt attribute in all images to improve accessibility.
- For example, here's a more semantic version of the HTML for this project:
<article class="products__card card">
<figure class="card__figure">
<picture class="card__picture">
<source media="(max-width:600px)" srcset="" />
<source media="(max-width:610px)" srcset="" />
<img src="" alt="Waffle" class="card__img" />
</picture>
<figcaption class="card__figcaption">
<span class="card__name">Waffle</span>
<h2 class="card__details">Waffle with Berries</h2>
<span class="card__price">$6.50</span>
</figcaption>
</figure>
<button class="card__btn">
<span class="card__addBtn">Add to Cart</span>
</button>
</article>
This would really improve your SEO. If I can give you some advice, when you're given designs, try to section them by "boxes" or "blocks" that contain other "boxes." In this case, each dish represents independent information, so I wrap it in an <article>
element, which makes the browser understand that this is an independent component representing a piece of information (with its own description, image, name, and price). This could be within a <section>
, and you can continue experimenting with this approach. It will give you more practice.
CSS:
-
The responsive design seems to be incorrectly applied :(. You could opt for structuring the dish cards using display: grid, with grid-template-columns: repeat(auto-fit, minmax($size, 1fr));
, and you will have more flexibility in how you apply the responsive layout.
-
I would also recommend a class methodology, like BEM, to avoid specificity issues in your CSS.
JS:
- The use of XMLHttpRequest is outdated. Today, most browsers support fetch(), which is more modern, easier to use, and more efficient:
function getcarts() {
fetch('data.json')
.then(response => response.json())
.then(data => {
let cartsCount = data.length;
createCarts(data, cartsCount);
let btnContainers = document.querySelectorAll(".cart .image");
addToOrder(btnContainers, data);
})
.catch(error => console.error('Error loading carts:', error));
}
- Repeated DOM Manipulation: Multiple DOM manipulations are happening within loops
(for example, in createCarts, addToOrder)
. This causes a lot of calculations and changes to the DOM, which can negatively impact performance, especially if you have a large number of elements. Use DOM fragments (document.createDocumentFragment())
to create elements in memory and then insert them all at once at the end.
function createCarts(obj, count) {
const fragment = document.createDocumentFragment();
for (let i = 0; i < count; i++) {
let cart = document.createElement("div");
cart.classList.add("cart");
cart.id = i;
fragment.appendChild(cart);
}
contnt.appendChild(fragment);
}
- Button Code Duplication: The buttons and their icons
(notAddBtn, addedBtn)
have a similar creation pattern. You're repeating a lot of code that could be simplified. You could create a generic function for creating buttons that accepts parameters to customize them.
function createButton(className, iconClass, textContent) {
let btn = document.createElement("button");
btn.classList.add(className);
let icon = document.createElement("i");
icon.classList.add(...iconClass.split(" "));
btn.appendChild(icon);
if (textContent) {
let text = document.createTextNode(textContent);
btn.appendChild(text);
}
return btn;
}
let notAddBtn = createButton("notAdd", "fa-solid fa-cart-plus", "Add to Cart");
let addedBtn = createButton("added", "fa-solid fa-plus");
General Recommendations:
- Avoid using inline styles and instead use CSS classes.
- Use unique identifiers instead of relying on indexes for logic and IDs.
- Optimize event handling by using event delegation.
I hope these recommendations were helpful. Keep it up, and if you ever have any questions or get stuck on something, feel free to reach out to me on LinkedIn.
Happy coding! 🍧