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 trialNatalie Cluer
18,898 Pointsa better way to do this?
I completed this challenge different than the instructor, to me this seems more clean and concise, but I was just wondering if there was something I'm missing? (any hidden pitfalls here?
var students = [ { name: 'Natalie', track: 'iOS', achievments: 34, points: 57474 }, { name: 'Rudy', track: 'Web Design', achievments: 65, points: 4848 }, { name: 'Buffy', track: 'Front-end Develpoment', achievments: 23, points: 9292 }, { name: 'Stanley', track: 'Java', achievments: 94, points: 5858585 }, { name:'Bobby', track: 'HTML/ CSS', achievments: 56, points: 5755 } ];
for (var i = 0; i < students.length; i++) { for (var prop in students[i]) { document.write([prop], ': ', students[i][prop] + '<br>'); } document.write('<br>'); }
3 Answers
Steven Parker
231,248 PointsYou don't need the brackets around the first "prop" in the document.write. The output may look the same but internally that creates an array you don't need.
As to the overall strategy, if you compare it to the video output you can see the results are a bit different. In particular:
- the item titles are different (capital first letters in the video)
- the order of the items is different ("Points" come before "Achievements" in the video)
Another difference can't be seen with this data set, but if one of the properties were missing, the video code would still display the title and show "undefined" for the value, where this code would just not show that line at all. On the other hand, if you were to add a new property to one or more records, the video code would ignore it but your code would display it.
So each method has it's own advantages and drawbacks. The choice would need to be based on knowledge of the differences and the specific requirement critera (not discussed in the video).
M Glasser
10,868 PointsDid this challenge the exact same way as you and came here to ask the same thing. Seems like less code to achieve a similar result.
Michael Sraj
23,188 PointsI did the challenge the same way you did but will have to agree with Steven Parker. Each way has its limitations, specifically when it comes to a value that doesn't exist. I personally think the way you did it is efficient provided that you can guarantee all of the values will exist.