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 JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Build an Object Challenge, Part 2

Mahfuzur Rahman
Mahfuzur Rahman
3,204 Points

Accessing the Property Names using an array. I've used a new array Junk to put the property names into it.

var student= [ { name:'arnold', track:'bodyBuilding', achievement:'MrMan', points:1900 }, { name:'bruce', track:'comedy', achievement:'MrMan', points:2000}, { name:'christopher', track:'architecture', achievement:'discovery', points:1600}, { name:'dave', track:'programming', achievement:'sharp', points:1980}, { name:'emanuel', track:'surviver', achievement:'goldenGlobe', points:2010} ];

for (var i=0; i<student.length; i++) { for (var key in student){ var junk=new Array(); junk.push(key); }

var name= junk[i]+student[i].name; var track= junk[i]+student[i].track; var achievement= junk[i]+student[i].achievement; var points= junk[i]+student[i].points; var message; message = name+'<p>'+track+'</p>'+'<p>'+achievement +'</p>'+points+'<p></p>'; document.write(message); }

Tim Strand
Tim Strand
22,458 Points

i reformatted the code below, but in your 2nd for loop you are initializing junk every time. move the var junk = [] declaration up above student (i usually add an s to indicate an array) also name is always going to be the last els info since you say i which === 5 at the time you are using it. if you want to write each one out then name and the document.write should be in your for loop. i think the answer i provided gets you to the same place with an easier to read logic

1 Answer

Tim Strand
Tim Strand
22,458 Points

just loop the students array and pull out each student. then access the students individual data.

const students = [ 
  { name:'arnold', track:'bodyBuilding', achievement:'MrMan', points:1900 }, 
  { name:'bruce', track:'comedy', achievement:'MrMan', points:2000}, 
  { name:'christopher', track:'architecture', achievement:'discovery', points:1600}, 
  { name:'dave', track:'programming', achievement:'sharp', points:1980}, 
  { name:'emanuel', track:'surviver', achievement:'goldenGlobe', points:2010} 
];
let junk = [];
students.forEach(function (student) {
  const message = `<p>${student.name}<br>${student.track}<br>${student.achievement}<br>${student.points}</p>`; 
  document.write(message);
});