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

Extra variables

I know everyone codes a little differently, but I'm still trying to learn the best practices. This is what I came up with and it works fine, but seems significantly different from what Dave did. Is there a reason he didn't put a loop inside a loop. It seemed to me like writing out each object property was a lot of repeating, so I made one loop for the array and one for each object in the array.

My bigger question though, is why the extra variables? Is that just for readability? Why make the variable student if it's just going to equal students[i]? And why do you need to make an outputDiv variable in the print function.

var message = '';

for (var i=0; i<students.length; i+=1) {  
  for (prop in students[i]) {
    message += "<p><strong>" + prop + ": </strong>" + students[i][prop] + "<p>";
  }
  message += "<br />";
}
document.getElementById('output').innerHTML = message;

2 Answers

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Here's what I think.

Use of less useful variables are just for readability and slight convenience. Some people thinks student variable is necessary because it indicates it will be used somewhere in the code and hints its purpose.

Not using nested loop is just a design decision since he wanted to give separate markup for student.name and capitalized property name to indicate fields. The difference in end result is minor at least in this case.

You can certainly question the use of outputDiv since it's not used more than once and used inside a function. It's not necessary to assign it to variable unless that function uses the variable multiple times. For example, if you needed to call doucment.getElementById('output') more than one time, it is painful and error prone.

There are many ways to accomplish things and in this case both are understandable to me. Personally, I decided to wrap each of complete student record in div and outer div to wrap the whole student records.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Brian,

Good job on the code.

Dave, depending on the lesson, teaches the bare-basics to accomplish something. As you seem to already know, there are many way to code the same thing (It's very good that you can see different and more efficient ways).

As students learn and advance, they will learn the shorter and DRYer way to code. I find it kind of like it was learning a language in school... first you learned "do not" and after... then your learned "don't." If you learnt "don't" first then "do not" would make no sense.

I thank you for sharing your code here in the community. This is the best place for that. Everyone can see the way you do it, compare it the way they are doing it... and everyone learns.

Keep it up and Keep Coding! :)