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 trialElias Hass
10,339 PointsMy solution to Build an Object Challenge
This took a while. Had a lot of trouble getting for loop to work.
const pet = [
{
name: "Charlie",
type: "Dog",
breed: "Australian Shepherd",
age: 7,
photo: "aussie",
},
{
name: "Luna",
type: "Dog",
breed: "Dachshund",
age: 2,
photo: "dachshund",
},
{
name: "Marty",
type: "Dog",
breed: "Golden",
age: 2,
photo: "golden",
},
{
name: "Percy",
type: "Cat",
breed: "Persian",
age: 6,
photo: "persian",
},
{
name: "Jack",
type: "Dog",
breed: "Pug",
age: 2,
photo: "pug",
},
{
name: "Milo",
type: "Cat",
breed: "Tabby",
age: 2,
photo: "tabby",
},
];
getPets = function (object) {
let displayPets = "";
//let petDirectory = []; <--- had a look again at this line. Made a silly error here.
//for (let prop in object) petDirectory.push(object[prop]); <--- And here too.
//Edit: Upon correction - petDirectory changed to object
for (let i = 0; i < object.length; i++) {
displayPets += `
<h2>${object[i].name}</h2>
<h3>${object[i].type} | ${object[i].breed}</h3>
<p>Age: ${object[i].age}</p>
<img src="img/${object[i].photo}.jpg" alt="${object[i].breed}">
`;
}
return displayPets;
};
let html = getPets(pet);
document.querySelector("main").innerHTML = html;