1

I have successfully assign 2 columns in a row, started from the number 9th element of array:

function format(d) {
   let obj = String(d).split(',').slice(9);
   var tbl='', res='';

   obj.forEach(function(item, index) {
      index % 2 === 0 ? game = item : value = item;
      index % 2 !== 0 ? res += '<tr><td>' + game + '</td><td>' + value + '</td></tr>' : '';
   });

   tbl = '<table class="w-100 table-sm nowrap table-bordered">';
    tbl += '<thead><tr><th>A</th><th>B</th><th>C</th></tr></thead>';
    tbl += '<tbody>' + res + '</tbody>';
    tbl += '</table>';
    return tbl;
}

However, I failed to use the same method to assign for 3 columns in a row. How do I use the % operator to design the 3 columns in a row?

I get the idea from andrew:

function format(d) {
       let obj = String(d).split(',').slice(9);
       var tbl='', res='', value='', value2='';

       obj.forEach(function(item, index) {
          index % 3 === 0 ? game = '<td>' + item + '</td>' : value2 = '<td>' + item + '</td>';
          index % 3 === 1 ? value = '<td>' + item + '</td>' : '';
          index % 3 === 2 ? res += '<tr>' + game + value + value2 + '</tr>' : '';
       });

       tbl = '<table class="w-100 table-sm nowrap table-bordered">';
        tbl += '<thead><tr><th>A</th><th>B</th><th>C</th></tr></thead>';
        tbl += '<tbody>' + res + '</tbody>';
        tbl += '</table>';
        return tbl;
    }
Wolfy
  • 23
  • 3
  • 2
    I don't get what is so difficult here? using `%3` and adding one more `` and `` won't work? – Vinay May 15 '20 at 16:22
  • 1
    In the above example, imagine if you changed `index % 2 !== 0` to `index % 2 === 1`. And then consider the equivalent approach for `index % 3`. – andrewJames May 15 '20 at 16:45
  • @andrewjames, great, thanks sir, I think I get your idea. I have solved the issue. I updated my solution again. – Wolfy May 16 '20 at 03:24
  • 1
    Great - glad you solved the issue. It is worth noting that the `%` operator (modulus/remainder) may behave in different ways, when you use it in different languages - see [here](https://stackoverflow.com/questions/10065080/mod-explanation). – andrewJames May 17 '20 at 00:37

0 Answers0