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

I've tried to DRY code, not really working

Hi. This is my code: for(var i = 0; i < students.length; i++) { document.write("<h1>" + "Student: "+ students[i].name + "</h1>"); document.write("<p>" + "track: " + students[i].track + "</p>"); document.write("<p>" + "achievement(s): " + students[i].achievement + "</p>"); document.write("<p>" + "point(s): " + students[i].points + "</p>"); }

I've tried to remove the: document.write and replace it with the function from the video. Didn't seem to work for me. Then i tried: var message = document.write. Didn't work either, why doesn't this work?

Matthew Rigdon
Matthew Rigdon
8,223 Points

Fixed code formatting:

for(var i = 0; i < students.length; i++) { 
document.write("" + "Student: "+ students[i].name + ""); 
document.write("" + "track: " + students[i].track + ""); 
document.write("" + "achievement(s): " + students[i].achievement + "");
document.write("" + "point(s): " + students[i].points + ""); 
}

1 Answer

Jess Taylor
Jess Taylor
5,160 Points

i think what your trying to do is as follows :

for ( i = 0; i < student.length; i++ ) {
    var message = "";
    message += '<h2>Student: ' + student[i].name + '</h2>';
    message += '<p>Track: ' + student[i].track + '</p>';
    message += '<p>Achievements: ' + student[i].achievements + '</p>';
    message += '<p>Points: ' + student[i].points + '</p>';

        // next line can be swapped with a function call to print instead
    document.write(message);
}