Design comparison
Solution retrospective
Feedback welcome!
What did you find difficult while building the project?
- Definitely a Figma design was going to be beneficial for this project. The thing that took the most time was figuring out the styling (spacing, colours etc.)
Which areas of your code are you unsure of?
- I've added JavaScript code to practice DOM manipulation and also to calculate the average score, depending on the score values inside of the data.json file. I would love to get some feedback if there are best practices that I can use and eventually make the code more efficient.
Community feedback
- @erlynascaratePosted over 1 year ago
Hello, nice work on the project!
You could also do this:
let view = '' data.forEach((item) => { ----scoreArr.push(item.score) ----const div = ` --------<div class="${item.category.toLocaleLowerCase()} summary__item "> ------------<div class="${item.category.toLocaleLowerCase()}__description summary__description"> ----------------<img src="${item.icon}" alt="${item.category} icon" /> ----------------<h3>${item.category}</h3> ------------</div> ------------<div class="summary__scores"> ----------------<span>${item.score}</span>/ ----------------<span>100</span> ------------</div> --------</div> ----` ----view += div }) summaryContent.innerHTML = view
Instead of this:
data.forEach((item) => { ----// ... (HTML generation) ----summaryContent.innerHTML += div })
And here's why
In the first snippet:
let view = '' data.forEach((item) => { ----// ... (HTML generation) ----view += div }) summaryContent.innerHTML = view
The code creates an empty
view
string and iterates over thedata
array, generating the HTML content for eachitem
and appending it to theview
string using string concatenation. Finally, after the loop, the entireview
string is set as theinnerHTML
of thesummaryContent
element. This means that the entire HTML content is built in memory before being added to the DOM.In the second snippet:
data.forEach((item) => { ----// ... (HTML generation) ----summaryContent.innerHTML += div })
The code directly appends the generated HTML content to the
summaryContent
element usinginnerHTML
. In this case, the HTML content is added to the DOM at each iteration of the loop. This approach can be less efficient because the DOM is modified multiple times, and each modification can trigger reflows and repaints.In general, the first approach with the
view
string can be more efficient because it reduces the number of DOM modifications. Building the entire HTML content in memory and then setting it once as theinnerHTML
of the target element is generally faster than appending individual elements one by one.However, if the
data
array is relatively small, the performance difference between the two approaches might not be significant. In such cases, you can choose the approach that you find more readable and maintainable. The first approach with theview
string might be considered more organized, as it separates the HTML generation from DOM manipulation.Marked as helpful0@alibeniaminaliPosted over 1 year ago@erlynascarate
Thank you for explaining and giving me a better solution. I already applied the feedback from you.
Kind regards, Ali
0
Please log in to post a comment
Log in with GitHubJoin 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