Ben
Ben

Reputation: 87

Can I split a pre-sliced string in javascript?

Hi I have a technical test that requires me to split 180 characters into 6 groups then place them into 6 tables.

I have split the characters thanks to help I got here a couple of days ago, but realise that I need to split the newly formed arrays again; as once the tables have been created, the 6 groups just log themselves into the top row of the first of the 6 tables.

I need the groups to log as pairs into the 9 col x 3 row tables and I don't seem to be able to get them to work. Here is my code:

 <script>

 var ticketString ="011722475204365360702637497481233455758302154058881928446789061241507324334876840738576186051132437816395663800818206590104559628214294664710935667287132130687703253151692742547985".match(/.{1,2}/g);

 function loadTickets () {

 var ticket = [];

 for (var i = 0; i < ticketString.length / 15; i++) {

     ticket[i] = ticketString.slice(i * 15, (i + 1) * 15).sort();

     var table = document.createElement("TABLE")

     table.border='1'

     table.setAttribute("id","ticketTable" + i)

     var tableBody = document.createElement("TBODY")

          tableBody.setAttribute("id","ticketBody")

          table.appendChild(tableBody); 

                 for (b=0;b<=2;b++) {

                 var row = document.createElement("TR")

                 row.setAttribute("id", "ticketRow" + b)

                 tableBody.appendChild(row)

                     for (c=0; c<=8;c++) {

                     var cell = document.createElement("TD")

                     cell.setAttribute("id","ticket-number" + c)

                     cell.bgColor = 'red';

                     row.appendChild(cell)

                     document.body.appendChild(table)

                      document.getElementById('ticket-number' + i).innerHTML = ticket[i];


     }

 }

 }

 console.log(tableBody);

 console.log(row);

 console.log(cell);

 };

 </script>

Upvotes: 3

Views: 72

Answers (1)

Tomanow
Tomanow

Reputation: 7357

I'm not sure how you are determining the empty boxes, but this is the 6 tables with 3 rows and 5 numbers in each row like in your picture. Essentially it does the following:

  1. Splits string into 18 arrays of 5 numbers each.
  2. Creates a multi-dimensional array of arrays of 3 of the above arrays created in #1.
  3. Checks each position based on algorithm determining if number belongs in that index.
  4. Creates a table for each outer array, a row for each inner array within that table, and a column for each number within each row within that table.

Visually you can think of it like stacking multi-dimensional arrays.

 var ticketString = "011722475204365360702637497481233455758302154058881928446789061241507324334876840738576186051132437816395663800818206590104559628214294664710935667287132130687703253151692742547985".match(/.{1,2}/g);

 function loadTickets() {
     var ticketTables = [];
     var ticketGroups = [];
     var numberOfNumbers = 5; // per row
     var inc = 0;
     for (var i = 0; i < ticketString.length / numberOfNumbers; i++) {
         var start = i * numberOfNumbers;
         var end = start + numberOfNumbers;
         ticketGroups[i] = ticketString.slice(start, end);

         ticketTables[inc] = ticketTables[inc] || [];
         ticketTables[inc].push(ticketGroups[i]);

         if ((i + 1) % 3 === 0) {
             inc += 1;
         }
     }

     for (var i = 0, max = ticketTables.length; i < max; i += 1) {
         var table = ticketTables[i];
         for (var j = 0, jmax = table.length; j < jmax; j += 1) {
             var row = table[j];
             var newRow = [];
             for (var k = 0, kmax = 9; k < kmax; k += 1) {
                 newRow[k] = '';
                 for (var l = 0, lmax = row.length; l < lmax; l += 1) {
                     var number = parseInt(row[l]);

                     if (number >= (k) * 10 && number < (k + 1) * 10) {
                         newRow[k] = number;
                     }

                     if (number >= (kmax * 10) && k === kmax - 1) {
                         newRow[k] = number
                     }
                 }

             }
             ticketTables[i][j] = newRow;

         }
     }

     console.log(ticketTables);

     for (var i = 0, max = ticketTables.length; i < max; i += 1) {
         var table = ticketTables[i];
         var $table = document.createElement('table');
         for (var j = 0, jmax = ticketTables[i].length; j < jmax; j += 1) {
             var row = table[j];
             var $row = document.createElement('tr');
             $table.appendChild($row);

             for (var k = 0, kmax = row.length; k < kmax; k += 1) {
                 var cell = row[k];
                 var $cell = document.createElement('td');
                 $row.appendChild($cell);
                 $cell.innerHTML = cell;
             }

         }
         document.body.appendChild($table);
     }
 };

 loadTickets();
input {
    width: 100%;
}
table, tr, td {
    border: 1px solid grey;
}
table tr td {
    min-width: 20px;
}

Upvotes: 1

Related Questions