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

Ashley McDonnell
Ashley McDonnell
1,228 Points

Only returning one name/value pair

Hi All,

I'm having troubles with the below code, it only prints out the last object in the array.

I have watched Dave's answer, to me it seems there must be a better wat but I can get mine to work.

Any Suggestions? This was my attempt:

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}
var name;
var track;
var achievements;
var points;

for (var details in students) {

 for (var i = 0; i < students.length; i += 1) {
  name = students[i].name;
  track = students[i].track;
  achievements = students[i].achievements;
  points = students[i].points; 
}
}

html = '<p>Name: ' + name + '<br> Track: ' + track + '<br> Achievements: ' + achievements + '<br> Points: ' + points + '</p>'; 
print(html);

2 Answers

Stephan Olsen
Stephan Olsen
6,650 Points

The reason why you only get a message with one person, is because for every iteration in your loop, you reassign the variable value. This means that after your loop has run through once, you have a message with the first students information. The next time it runs through, it reassigns your variables with the information of the second student. This ultimately results in the last person being printed out. For more in-depth answer, you can check out this post: https://teamtreehouse.com/community/why-only-the-last-student-appears

Ashley McDonnell
Ashley McDonnell
1,228 Points

Thanks Stephan!

I looked at the example you've provided and modified my print function accordingly but to no avail.

I have a feeling that perhaps my for in loop is incorrect.

I have a feeling it's because I'm not creating a variable inside the loop to store the values after each iteration, but I amended that and that did not work.

To concatenate each of the properties of the loop and print to the page, does this have to be included inside the loop to get all of the objects to print?

(cleaned up my syntax a little)

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML += message;
}
var name;
var track;
var achievements;
var points;
var message= "";

for (var details in students) {
 for (var i = 0; i < students.length; i += 1) {
 var list = name = students[i].name;
  track = students[i].track;
  achievements = students[i].achievements;
  points = students[i].points; 
}
}

html = '<p>Name: ' + name + '<br> Track: ' + track + '<br> Achievements: ' + achievements + '<br> Points: ' + points + '</p>'; 
print(html);
nico dev
nico dev
20,364 Points

Hey,

Awesome effort there and awesome answer of Stephan, too! I just jump in to give you yet another hint(s)... since I see you have the wish and all it takes to solve it by yourself.

a) Did you notice that you have two for loops? The first one checks the whole students array, and the second one, inside the former, checks each student? So the second one resets each time you start checking a student... but where does that leave the first for loop?

b) Remember that whereas a = a + 1, you could to that by simply doing a += 1. And that also applies to strings.

Well, I hope that helps a little bit more. :) I trust you may be on the way with this additional hints. But feel free to come back and follow up otherwise!

Ashley McDonnell
Ashley McDonnell
1,228 Points

Thanks for that Nico!

I kind of worked it out while bouncing it off a friend.

I was trying to complicate things by not doing the string concatenation inside the function.

Thanks for your help!

nico dev
nico dev
20,364 Points

Great stuff! I knew you'd get there... you were already so close! Glad if I helped.

And especially congrats on your effort, determination and persistence. That makes the whole difference.