E-COMMERCE PRODUCT PAGE HTML | CSS | JS

Solution retrospective
- This is one of the most challenging projects i have taken on in a while and i feel so good to have at least taken an attempt to making my solution as close as possible to the design.
- I know there are always better ways to do things and there is always room for improvements but honestly, i cannot see how i could have done things differently.
- It was a hassle trying to implement both the lightbox and the carousel / slideshow but with the help of Stack overflow and W3 schools, i was able to tackle the problem and provide a good solution.
- The code base is quite lengthy so kudos to who takes out time to assess my solution, i would like alternative approaches to the problem and also best practices.
Please log in to post a comment
Log in with GitHubCommunity feedback
- @skyv26
Hi @BeeAlmighty,
You've done a commendable job putting together the JavaScript for this project! The code works well and showcases your learning journey. 🚀 However, there are a few areas where you can improve to make the code cleaner, more readable, and aligned with best practices. Here are my suggestions:
🛠 Code Suggestions:
-
Use Arrow Functions:
Instead of traditionalfunction
declarations, you can simplify with arrow functions for consistency and brevity. For example:const openModal = () => { document.getElementById("myModal").style.display = "block"; };
-
Follow the DRY Principle:
There are repetitive code patterns, especially in theshowSlides
andshowSlides2
functions. Consider creating a helper function that handles both scenarios dynamically based on parameters (like the class names or indices). -
Shortcuts for Querying Elements:
Instead of callingdocument.querySelector
repeatedly, you can store frequently accessed elements in a central object or use a utility function for querying:const $ = (selector) => document.querySelector(selector); const sidebar = $('.nav--sidebar');
-
Add Meaningful Variable Names:
Some variable names (e.g.,docBod
,docBody
,itemCart
) could be more descriptive. Consistent naming will make the code more intuitive. -
Reduce Global Variables:
To avoid potential conflicts and improve modularity, wrap the related code into a single object or IIFE (Immediately Invoked Function Expression). -
Spacing for Readability:
Add blank lines between sections of your code. For example, separate sidebar-related code, cart operations, and carousel code into distinct blocks.
💡 Best Practices:
- Add Comments: Briefly explain what each function or block of code does. This will make it easier to understand and maintain.
- Group Logic: Use modular design by separating different functionalities (e.g., cart, sidebar, carousel) into distinct classes or modules.
- Event Delegation: For elements like
addCartBtn
ordelIcon
, you can use event delegation to attach a single listener to a parent element.
🏆 Overall Feedback:
- Your code demonstrates a solid understanding of DOM manipulation and event handling. 👏
- Although the script is lengthy, it’s effective. Refactoring the repetitive parts will make it more concise and maintainable.
- The output is impressive, and it’s clear you’ve put a lot of effort into learning through this project!
⭐ Action Plan:
- Refactor repetitive logic like
showSlides
andshowSlides2
into a single function. - Use arrow functions and reduce redundant selectors with utilities like
querySelector
. - Modularize your code into logical sections for improved readability and debugging.
Let me know if you need help implementing these improvements. You're on the right track—keep going! 🌟
Best,
Aakash 😊Marked as helpful -
Join our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord