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 1

Euangelos Kolimitras
seal-mask
.a{fill-rule:evenodd;}techdegree
Euangelos Kolimitras
Full Stack JavaScript Techdegree Student 3,307 Points

Unable to loop throught an array of objects

Greetings fellows,

I am dealing with an issue today. My problem is that i created the script to loop throught that particular array but the problem is tha it only gives back the last element of the array.Maybe soone can spot that 'tiny' mistake. Thanks

CODE BELOW: var students = [ { name : "John" , track : "Web development", achievments : 12, points : 251
}, { name : "Mary" , track : "IOs development", achievments : 55, points : 352
}, { name : "Euan" , track : "Anroid development", achievments : 32, points : 361
}, { name : "George" , track : "Database development", achievments : 65, points : 870
}, { name : "Ricky" , track : "Marketing development", achievments : 33, points : 3315
} ];

for(var info in students){ var x = students[info]; console.log( x.name + " " + x.track + " " + x.achievments + " " + x.points); showResult(x.name , x.track , x.achievments , x.points); }

function showResult(n , t , a , p){ var showToDoc = document.getElementById("output"); showToDoc.innerHTML = '<ol><li>' + "I am " + n + " and i follow the track of " + t + ". Currently i have " + a + " achievments with the amount of " + p + " points." + '</li></ol>'; }

2 Answers

Ari Misha
Ari Misha
19,323 Points

Hiya there! Your "students" variable is an array of objects. When we loop through an object in JS, logging out the alias variable gives us "properties/keys" right? But in this case you dont have properties , you have objects. So in order to access 'em properties , you're gonna have to loop it twice. Both should nested. And first loop is going to be a "for" loop for an array and second loop is going to be a "for-in" loop for an object , right? You're gonna have to adjust some changes in your code but im pretty sure it'll work. Good luck! (: