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 trialBen McMahan
7,922 PointsHow is this solution? Building off past sections
let html = '';
for (let pet in pets) {
let name = pets[pet].name;
let age = pets[pet].age;
let type = pets[pet].type;
let breed = pets[pet].breed;
let photo = pets[pet].photo;
html += `
<h2>${name}</h2>
<h3>${type} | ${breed}</h3>
<p>${age}</p>
<img src="${photo}" alt="${name}">
`;
}
document.querySelector('main').innerHTML = html;
1 Answer
Rohald van Merode
Treehouse StaffHi Ben McMahan 👋
It would depend on how your pets
array is set up but I assume this is an array of pet
objects? If so you could select the properties using the pet
variable you declared in the for/in
loop. You could then for example change pets[pet].name
to just be pet.name
.
Other than that everything looks perfect to me! 😄 Keep up the great work 🔥
Ben McMahan
7,922 PointsBen McMahan
7,922 PointsThanks, I think my post ended up in the wrong section - one of the steps in the Full Stack JavaScript track.
It's interesting, in the past working with things like Vue.JS, I've always done something like
pet.name
, however in this case that results inundefined
in the output without usingpets[pet]
. Here is the array:And here is a refactored version of my code: