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

Cisco Rendon
PLUS
Cisco Rendon
Courses Plus Student 37,688 Points

I am trying to create a name table using javascript. I created the rudimentary table but having a hard time

I am trying to create a table with names only, a dynamic table. I can create it in standard html and style it with css but that will take a long time especially when you have a lot of names. We may have covered it in a js class but I cannot remember if we did or didn't. Has anyone come across something like this before? This is my code for the basic table layout, it's plain / vanilla js.

var table = '';
var rows = 2;
var cols = 4;
for (var r = 0; r < rows; r++) {
    table += '<tr>';
    for (var c = 0; c < cols; c++) {
        table += '<td>' + c + '</td>';
    }
    table += '</tr>';
}
document.write('<table>' + table + '</table>');

2 Answers

Steven Parker
Steven Parker
231,236 Points

This code will effectively create a table with multiple rows and columns.

So what exactly are you having a hard time with?

Cisco Rendon
Cisco Rendon
Courses Plus Student 37,688 Points

Steven Parker i am trying to add the names thru a set of variables as well. Just names, not an age column, nothing to technical yet just names. That is where I am hitting the way. Only way i was thinking of was creating an array of names and inserting that into the variables but it wasn't working for me.

Steven Parker
Steven Parker
231,236 Points

That sounds like a good idea, but where are the names? I don't see any in this code.

Steven Parker
Steven Parker
231,236 Points

An array should work fine, but it should have the same number of items as the table has cells:

var table = '';
var rows = 2;
var cols = 4;
var names = ["Katie", "Ava", "Zoe", "Lacey", "Tucker", "Rusty", "Rudy", "Mimi"];  // 8 names
for (var r = 0; r < rows; r++) {
    table += '<tr>';
    for (var c = 0; c < cols; c++) {
        table += '<td>' + names.shift() + '</td>';  // put a name in each cell
    }
    table += '</tr>';
}
document.write('<table>' + table + '</table>');
Cisco Rendon
PLUS
Cisco Rendon
Courses Plus Student 37,688 Points

Hi Steven Parker, sorry for the late reply, been busy on a project that side tracked me from this topic. You are asking the question of where are the names and that is where I am lost. Let's say i want to add 10 names in an array and insert the array into the js code. That is where I am stuck / lost, where do i add the names. Even better, how do I add the names?