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

Accessing objects

While my code work fine, but if i have something like this javascript Track:"Web Design, Front End , JS" it output <b>undefined<b> I am accessing it as

for (var i = 0; i< students.length; i++){
    student = students[i];
    //document.write(student);
    //message = "<h4>Student Name : "+ student.Name + "</h4>";
    message = "<p> " + student.Tracks + "</p>";
    document.write(message);
}```

and my student.js is as follow
```javascript
var students = [
    {
        Name:"a",
        Track:"Web Design, Front End , JS",
        Achievements:11000,
        points:"More than 10k"
    },
    {
        Name:"b",
        Track:"iOS",
        Achievements:"Marv",
        points:"Starting"
    },
    {
        Name:"c",
        Tracks:"VR",
        Achievements:"Getting started",
        points:"0"
    },
    {
        Name:"d",
        Tracks:"None",
        Achievements:0,
        points:"Starting"
    }
]

1 Answer

Steven Parker
Steven Parker
231,275 Points

:point_right: Are you using the same property names as defined in student.js?

You didn't show that part of your code, but unless you deviated from the video, the property names you are using are not part of the student object.

Your code above is attempting to access student.Tracks (plural, and capital "T"), but the video uses student.track (singular, lower-case "t").

"What's in your wallet student.js?" :point_left: Joke based on American TV, in case you're from elsewhere and not familiar

Steven Parker sorry for not posting the other filer. I have updated my question. Have a look now.

Steven Parker
Steven Parker
231,275 Points

Aha - here's the cause :point_right: The propery names in student.js are not consistent.

The first two items have a property named "Track" (singular), and the last two have a property named "Tracks" (plural).

Your code is looking for student.Tracks, which will be undefined in the first two items.