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 trial

JavaScript

Ben McMahan
Ben McMahan
7,922 Points

How 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hi 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
Ben McMahan
7,922 Points

Thanks, 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 in undefined in the output without using pets[pet]. Here is the array:

/*
  Create an array of 'pet' objects.
  Each object should have the following properties:
  name, type, breed, age, and photo
*/
const pets = [
    {
        name: "Murphy",
        age: 1,
        type: "Dog",
        breed: "Blockhead",
        photo: "/img/aussie.jpg"
    },
    {
        name: "Hotdog",
        age: 2,
        type: "Dog",
        breed: "Dachshund",
        photo: "/img/dachshund.jpg"
    },
    {
        name: "Goldie",
        age: 6,
        type: "Dog",
        breed: "Golden",
        photo: "/img/golden.jpg"
    },
    {
        name: "Asia",
        age: 5,
        type: "Cat",
        breed: "Persian",
        photo: "/img/persian.jpg"
    },
    {
        name: "Puggy",
        age: 8,
        type: "Dog",
        breed: "Pug",
        photo: "/img/pug.jpg"
    }
];

And here is a refactored version of my code:

let html = '';

for (let petProp in pets) {
    let pet = pets[petProp];

    html += `
    <h2>${pet.name}</h2>
    <h3>${pet.type} | ${pet.breed}</h3>
    <p>${pet.age}</p>
    <img src="${pet.photo}" alt="${pet.name}">
    `;
}

// Notice the new way of adding the HTML. More efficient than innerHTML.
document.querySelector('main').insertAdjacentHTML("beforeend", html);