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

Kenneth Kim
Kenneth Kim
3,795 Points

Which is correct student[i].property OR student[i][property] ?

var students = [ 
  { 
    name: 'Dave',
    track: 'Front End Development',
    achievements: 158,
    points: 14730
  },
  {
    name: 'Jody',
    track: 'iOS Development with Swift',
    achievements: '175',
    points: '16375'
  },
  {
    name: 'Jordan',
    track: 'PHP Development',
    achievements: '55',
    points: '2025'
  },
  {
    name: 'John',
    track: 'Learn WordPress',
    achievements: '40',
    points: '1950'
  },
  {
    name: 'Trish',
    track: 'Rails Development',
    achievements: '5',
    points: '350'
  }
];


function printInfo(message){
    var outputDiv = document.getElementById('output');
    outputDiv.innerHTML = message; 
}


function studentInfoPrinter(){
    var message = "";
    for(var i = 0; i < students.length; i++){
        for(var prop in students[i]){
            message += "<p><b>" + prop + ":</b> " + students[i].prop + "</p>" // buggy?
            message += "<p><b>" + prop + ":</b> " + students[i][prop] + "</p>"
        }
        printInfo(message);
    }   
}

studentInfoPrinter();

Hi, after learning different techniques in the earlier videos, I've come up with this code in hopes of reducing code length. However, I've come across a weird problem.

Please help. I don't see why students[i].prop is not working properly. Sometimes it works but most of the time it does not. It display the property name but he value will be undefined. ex (name : undefined) Because of that I decided to use students[i][prop] in displaying the values. Can someone help me and explain why the former is not working? (but it does sometimes @_@)

1 Answer

Steven Parker
Steven Parker
231,269 Points

Your code doesn't show the structure of the students array.

But we can assume that it doesn't always contain a property named "prop". But maybe it does sometimes, which could explain why that code "works" sometimes. But it wouldn't be doing what you really want. It would always display the value of the "prop" property along with the other property names.

Your loop is clearly intended to iterate the properties, whatever they may be named. So the correct way to reference their values would be "students[i][prop]".

And to make posted code more readable, please use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:

Kenneth Kim
Kenneth Kim
3,795 Points

Hi Steven. Thank you for answering my question. I have updated my question and included the structure of the student. I've forgotten to included it because it was in stored in a different .js file.

Steven Parker
Steven Parker
231,269 Points

It's basically what I imagined when I wrote the answer above. But I'm not sure why "students[i].prop" would ever be anything but undefined with this data.