Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPiotr Manczak
Front End Web Development Techdegree Graduate 29,363 Pointshtml and loop
I think the html variable should be declared before the loop and then a string should be build up by adding next card each time the loop runs.
let html = "";
for(let i = 0; i < array.length; i++){
// 7-b. Inside, create a conditional checking if `i` is...
if(i >= start && i <= end){ // - greater than or equal to the start variable
html += `
<div class="author-card">
<div class="card-header">
<img src="${array[i].image}" alt="photo of ${array[i].name}" />
</div>
<div class="card-content">
<h2 class="title">${array[i].name}</h2>
<p>
${array[i].text}
</p>
</div>
</div>
`;
}
}
authorContainer.insertAdjacentHTML("beforeend", html);
... and then add newly created string to the index.html Is it not what we were tought? Which way is better?
1 Answer
Steven Parker
231,268 PointsDid you try this? You should have noticed that it displayed many duplicate cards, since the insertAdjacentHTML method appends the current code to page and this code change also appends the card codes inside the html variable.
It would, however, work if you also modify the later code to make it replace the page content instead of adding to it. Then the accumulation would still occur in just one place, but now in the variable instead of on the page. Both methods are equally effective at getting the job done.
There's rarely only one way to accomplish a particular task, and the fact that you're thinking of alternative strategies is evidence of your learning progress and grasp of the material.