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!

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

Najee Henderson
Najee Henderson
2,045 Points

I need help with my code... How can I improve on my code?

My code runs but it only list the values in the array and not the name set for the values (like achievement, points , track and name). I usually find a way to get through the challenges, but I'm stuck on this one.

var students = [ { name: 'Najee Henderson', Track: 'Fullstack Development,JavaScript, Swift, ApI, Neural Networks', Achievements: 4, Points : 1348 }, { name: 'Anne Brickman', Track: ' Swift , Neural Networks', Achievements: 7, Points : 678 }, { name: 'Booty Brown', Track: 'Rapping', Achievements: 345, Points : 1 }, { name: 'Fat Lip', Track: ' ApI, Neural Networks', Achievements: 787, Points : 1348 }, { name: 'Albert Einstien', Track: 'Genius: Legend', Achievements: 40000, Points : "Infinity" }];

for( var n in students){

document.write('<h2>' + students[n].name + '</h2>') document.write( '<p>' + n,':', students[n].Track +'</p>') document.write( '<p>' + n,':', students[n].Achievements + '</p>') document.write( '<p>' + n,':', students[n].Points + '</p>')


1 Answer

Hello! You'll have to manually type those out and then put the code after the titles with the object values. And it makes the code a bit easier to read, if you make a separate function that prints the code out to the page.

I think it's easier to use a regular for loop in this case because it'll be easier to cycle through the array of objects Hope that helps! For example:

var html = " ";
for(var i = 0; i < students.length; i++) {
  html += "<h2>Student: " + students[i].name + "</h2>";
  html += "<p>Track: " + students[i].track + "</p>";
  html += "<p>Achievements: " + students[i].achievements + "</p>";
  html += "<p>Points: " + students[i].points + "</p>";

function print(input) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = input;
Najee Henderson
Najee Henderson
2,045 Points

Ahh I see... ok thank you for the help...it worked just like it needed to