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

JavaScript Loops, Arrays and Objects> The Build an Object Challenge

I was thinking about using two loops to do the job of printing out the student record. However it does not seem to pass. Any suggestions about how to improve this code?

var message = '';
var student; 

function print(message) {
    var outputDiv = document.getElementById("output");
    outputDiv.innerHTML = message;
}

for (var i = 0; i < students.length; i + = 1) {
    student = students[i];
    for (var prop in student){
            message += '<p>' + prop + ': ' + student[prop] + '</p>';
    }
}
print(message);

1 Answer

Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey Ethan,

good job! You only made one small mistake that breaks your code: You wrote a space between the "+" and "=" in the addition assignment in your loop.

It should work like this:

for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    for (var prop in student){
            message += '<p>' + prop + ': ' + student[prop] + '</p>';
    }
}

Here is the working example.

Thanks Tobias! I was wondering what was wrong with this code for a long time. It was this one single space.

Tobias Helmrich
Tobias Helmrich
31,602 Points

No problem, I'm glad I could help! :) It's often the smallest thing that breaks our whole code.