Home > Blockchain >  Creating a special characters table without any headers
Creating a special characters table without any headers

Time:06-02

I have 128 special characters in an array. I'm trying to create a special character table without any headers that is 16 cells long and has 8 rows and I'm having trouble with the logic of the loops I'm creating. I've removed the part that are creating the table as that isn't relevant.

var rowCount = 8;

// For each Row
for (var r = 0; r < rowCount; r  ) {
  for (var c = (0   (r * 8)); c < (r * 8)   16; c  ) {
    console.log(c);
  }
}

So for each row, I'm trying to loop through 16 items of the 128 char array. The first loop I have, c prints out 0-15 as I'm expecting but when r enters its second iteration, c begins printing from 8-23, which is not what I want. I'm trying to get it to print what's following the previous iteration so 16-32, and then the third iteration 33-49 and so forth.

Can anyone explain to me what I'm doing wrong and how to correct my logic please?

CodePudding user response:

Instead of doing any of the complicated math in the nested loop declaration for the columns, keep that part simple 0-15 and do the math after. Where the for loop begins and ends ultimately doesn't matter because there are still always the same number rows and columns (since you've statically declared them).

i.e., you're trying to accomplish this on the inner loop iterations:

  • for(var c = 0; c < 16; c ){}
  • for(var c = 16; c < 32; c ){}
  • for(far c = 32; c < 48; c ){}

The actual number that the for loop begins and ends at is ultimately arbitrary, what matters is that you are able to calculate the number of times it should iterate... in this case 16 times for the columns. So 0-16 is all you need.

var rowCount = 8;
// For each Row
for(var r = 0; r < rowCount; r  ){
    // opening <tr>
    for(var c = 0; c < 16; c  ){
        console.log( (r * 16)   c );
        console.log("r"   r   "c"   c);
        // do <td> stuff
    }
    // closing </tr>
}

Note: Snippet cuts off some of the earlier console log so you don't see the beginning so here is a fiddle. https://jsfiddle.net/u7fLdt2w/

  • Related