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 Solution

Shane Meikle
Shane Meikle
13,188 Points

Wouldn't it have been better to use the for loop introduced in this course for this projects completion?

Not debating that the shown solution is valid, but wouldn't it have been better to utilize the for loop that was introduced as the viable solution to this challenge? As it stands, if something is added or removed, the changes will not be shown.

What I did:

for (var i = 0; i < students.length; i++)
{
  document.write('<h4>' + students[i].name + '</h4>');
  for (var prop in students[i])
  {
    document.write(prop + ": " + students[i][prop] + "<br/>");
  }

}

Appears to work while allowing for future changes to the objects. So I was wondering why the solution didn't use this result, or if I missed the reasoning somewhere?

1 Answer

I think your solution is a clever one, but I could see why someone would write it either way. If eventually new properties are added, you may or may not want them inside this loop, and it may be better to specify exactly which properties you would like to display using this loop. This could go either way, and I suppose it depends on the context of the situation.

I think in this case, I would use the original for the sake of readability. Just by reading the code, I can tell exactly what's being printed out, and therefore debugging it would be easier. Sometimes readability will take precedence over smaller code. Performance difference is most likely the same if not negligible.

Readability is probably why Dave chose to write it this way. From a learning perspective, it's easier for students to visually see what's going on.

Good post!

Shane Meikle
Shane Meikle
13,188 Points

Thanks for the response :)

I wasn't looking to debate the validity of the result offered by Dave, as it does get the job done. I was just caught off guard by the lack of the use of the for loop designed specifically for objects, and wasn't sure if it was due to it being bad practice to do it the way I did or something of the sort.

Sure thing! Being able to find multiple solutions to the same problem is a very handy skill to have. I enjoy seeing posts like these, they demonstrate a better understanding of the task given and it's fun to read the different solutions people come up with. Don't be afraid to post more of your own solutions in the future! :)