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

olu adesina
olu adesina
23,007 Points

can i use a for in loop with in a for loop

var students =[

  {name:'Olu Adesina',
  track:'javaScript',
  achievements:'loops,objects',
  points:1568},

  {name:'Olu Adesina',
  track:'javaScript',
  achievements:'loops,objects',
  points:1568},

  {name:'Olu Adesina',
  track:'html',
  achievements:'loops,objects',
  points:1568},

  {name:'Olu Adesina',
  track:'javaScript',
  achievements:'loops,objects',
  points:1568},

  {name:'jack Adesina',
  track:'javaScript',
  achievements:'loops,objects',
  points:1568},


]
  var message;

  for(x=0;x<students.length;x+=1){
      for(var name in[x]){
      message+= name, ':',[x][name]     
  }
}

  document.write(message)
Steven Parker
Steven Parker
229,921 Points

This question appears to be a duplicate of this other one.

Note to staff/moderators: I'm fine with it being deleted along with my answer.

1 Answer

Steven Parker
Steven Parker
229,921 Points

Sure you can.

But I don't think your inner loop is properly formed. And if I understand what you're intending to do here, you probably meant to write something like this:

for (x = 0; x < students.length; x += 1) {
  for (var name in students[x]) {
    message += name + ": " + students[x][name] + "<br>";
  }
  message += "<br>";
}

Also, since you want to append to it, when you declare message you should initialize it with an empty string.

olu adesina
olu adesina
23,007 Points

why is 'undefinedname: Olu Adesina' coming up on the first line when i run this code

Steven Parker
Steven Parker
229,921 Points

You must have overlooked my hint about initializing message when you declare it:

  var message = "";