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 trialdan parker
3,762 PointsI was wondering about the quality of this code...........?
var students = [
{ Name: 'Joe Blow',
Track: 'Front End Development',
Achievements: 34,
Points: 2345},
{ Name: 'Jane Fonda',
Track: 'Web Design',
Achievements: 14,
Points: 5846},
{ Name: 'Sam Blam',
Track: 'iOS',
Achievements: 45,
Points: 1247},
{ Name: 'Jimmy Stoopa',
Track: 'Front End Development',
Achievements: 89,
Points: 12548},
{ Name: 'Suzei Q',
Track: 'iOS',
Achievements: 25,
Points: 985}
];
for ( x = 0; x < students.length; x+= 1 ) {
var arr= students[x];
for ( prop in arr) {
document.write( prop + ': ' + arr[prop] + "<br>");
if ( prop === 'Points') { document.write("<br>");}
}
}
dan parker
3,762 Pointsthe array is students......var arr=students[x]
dan parker
3,762 PointsI've gotta learn how to present code in this forum.....I started playing around with the preview but i have a feeling there is more to it than just that
Niclas Valentiner
8,947 PointsWhen you run through the first for loop, you're taking students[x], a single value, and saving it in arr.
arr is a variable, not an array since your second for loop (the for in) then tries to run on a non-array.
You're basically running a for in on a single value variable that you change once every time you go through the first for loop.
dan parker
3,762 Pointsfor ( x = 0; x < students.length; x+= 1 ) {
<br>
var arr= students[x];
<br>
for ( prop in arr) {
<br>
document.write( prop + ': ' + arr[prop] + "<br>");
<br>
if ( prop === 'Points') { document.write("<br>");}
<br>
}
<br>
}
dan parker
3,762 Pointsthe code does do what i expected it to......but is the assignment of the array to a variable not good practice?
dan parker
3,762 Pointsthe for in loop is meant to run on the object(s) inside the array
Niclas Valentiner
8,947 PointsNever mind me... I'm forgetting that you've got an array of objects. That bit is fine!
Better variable names wouldn't hurt though. For the for loop variable i is generally used (short for index) and calling your object variable 'arr' is misleading. It'd be better to call it 'student' for example or 'studentObj'
dan parker
3,762 PointsI just realized that arr was misleading as you were reviewing this with me. Thank you for your time and help. I think it could be more versatile if I had indexed the last item in each object( somehow) to create the line breaks. Thanks again for taking the time to look at this
1 Answer
Iain Simmons
Treehouse Moderator 32,305 PointsAside from the naming issues that you've already had pointed out to you, you should also declare the loop index variable (in your case, x
) and the prop
variable before you use them in the loops.
Also, instead of checking for the Points
prop (which is unreliable since for..in
loops don't necessary go in the order you would expect), you should just put the additional br
tag outside of the for..in
loop but inside the outer for
loop. i.e. printing it for each student, not each time you come across a particular property of that student object.
Niclas Valentiner
8,947 PointsNiclas Valentiner
8,947 PointsA better structure of the code itself would be appreciated but it doesn't look like your for in loop should work.
As for the code structuring, in the bottom right corner of the textarea for comments, answers and questions is an eye icon. It's for previewing and it's really helpful with getting your question to look good.