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 Multiple Items with Arrays Using For Loops with Arrays

david mchale
david mchale
1,434 Points

Where am i goin wrong with printing out the arrays items??

var students = ['alan', 'john', 'paul', 'dave'];

var names = "";


names = "<ul>"; 

for (var i = 0; i < students.length; i++) {
        names += "<li>" + i + "</li>"; 
}

names += "</ul>"; //

console.log(names);

Moderator edited: Added markdown to the code so that it renders properly on the forums.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You are adding the index to the <li> instead of the array value at that index. I feel fairly sure that what you want is:

 names += "<li>" + students[i] + "</li>"; 

This should add the student name that resides at that index in the students array as opposed to just the index.

On a side note, I took the liberty of adding some markdown to your code so that it renders properly. If you have a moment, you might want to check out the Markdown Cheatsheet link at the :arrow_down: bottom of the "Add an Answer" box for instructions on how to post code properly to the forums and some other neat formatting for your posts.

Hope this helps! :sparkles: