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

Richard Verbraak
Richard Verbraak
7,739 Points

My solution being too simple ?

I just used a forEach loop and did it the lazy way with writing multiple document.write functions but it did the trick. Is this considered bad practice or lazy?

My code:

for (var prop in students) {
  document.write("<h2>Student: " + students[prop].name + "</h2>");
  document.write("<p>Track: " + students[prop].track + "</p>")
  document.write("<p>Achievements: " + students[prop].achievements + "</p>")
  document.write("<p>Points: " + students[prop].points + "</p>")
}

2 Answers

If it works it works! It depends on the scope of the project you are working on. If you have a project with a rigid structure and you only need a function for a very specific task, workarounds like this will do fine.

For some more complex projects with lots of moving parts, keeping your code modular and easy to swap values and variables is key. Good job thinking outside the box though!

Dylan

If you're resorting to self-described laziness, then you aren't learning.

And no, this wouldn't fly in a work environment. Code this verbose looks amateur; especially since you're doing pointless work by iterating over the students collection, getting a prop, and then using that prop to access what you want in the students collection. One of the usages of javascript objects (and maps in general) is to avoid the need for iterators by using a dictionary style lookup. (Sometimes we have to iterate over maps to turn them into other collections, but that's a different topic).