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

for looping returning NaN instead of array contents

My for loop is returning NaN rather than the content of my array. I did not ask it isNan() so I am confused as to why it is doing this. When I console.log students the array prints fine, (noted out here for testing) so I know its my loop.

var students = [
{
    name: "Brigette",
     track:"Front End Development", 
     achievements: '28',
     points: '2509'
    },

{
    name: "Thelma", 
    track: "Design",
    achievements: '9', 
    points: '667'
},

{
    name: "Kenneth", 
    track: "Front End Development", 
    achievements:'50', 
    points: '4572'
},

{
    name: "Jihoon", 
    track: "Android", 
    achievements:'49', 
    points: '3883' 
},

{
    name: "Samuel", 
    track: "Front End Development", 
    achievements: "6", 
    points:"352"}
];

// console.log(students);

//loop through array to print student & properties 

for ( i=0; i < students.length; i+= 1) {
        console.log([i][0] + [i][1] + [i][2] + [i][3])

}

2 Answers

Darren Joy
Darren Joy
19,573 Points

Nan is 'not a number', and for some reason your points and achievement you have created as strings and not numbers... first step would be leave out the quotations..

the second thing is this is an array of objects, so you would call the object as student[i] but then trying to draw out properties you'd have to use object notation like student.name to get the name property...

so student[0].name would return Brigette etc

Hope this helps give you an idea of what to change

So after feedback I figured it out. I want to post hear just in case someone else has this problem in the future. My problem was that I need arrayname[i].var in the loop rather than [i][position in array] . I added in the <p> and spaces to make it easier to read the final product.

//array named students with name, track, achivements (numbers ), and points at least 5 students 
var students = [
{
    name: "Brigette",
     track:"Front End Development", 
     achievements: '28',
     points: '2509'
    },

{
    name: "Thelma", 
    track: "Design",
    achievements: '9', 
    points: '667'
},

{
    name: "Kenneth", 
    track: "Front End Development", 
    achievements:'50', 
    points: '4572'
},

{
    name: "Jihoon", 
    track: "Android", 
    achievements:'49', 
    points: '3883' 
},

{
    name: "Samuel", 
    track: "Front End Development", 
    achievements: "6", 
    points:"352"}
];

//console.log(students);

//loop through array to print student & properties 
 var name;
 var track;
 var achievements;
 var points;

for(var i=0; i < students.length; i+= 1) {
    //access each sstudent array with students[student][prop] and return all so return student[][0] return [1]} etc?
        console.log("<p>" + students[i].name + " " + students[i].track + " "+ students[i].achievements + " " + students[i].points + "</p>");
}
Darren Joy
Darren Joy
19,573 Points

right on right on well done!