I have an array of data that has been collected by iterating through an existing table as such:
---------------
Mon | Hot | 12
---------------
Tue | Cold | -2
---------------
Wed | Warm | 3
---------------
Thu | Cold | -4
---------------
Fri | Cold | -3
---------------
The data in the array is stored as such: ['Mon', 'Hot', '12', 'Tue', 'Cold', '-2' .. etc ..]
The data now needs to be displayed in a table that has flipped the rows and columns, so that the data is shown as follows:
Mon | Tue | Wed | Thu | Fri
-------------------------------
Hot | Cold | Warm | Cold | Cold
-------------------------------
12 | -2 | 2 | -4 | -3
I am struggling to find the right loop statements to print the data correctly.
So far I have the following:
for (i = 0; i < weather_data.length; i++)
{
if (i%5==0)
{
table.push('<tr><td>'+weather_data[i*3]+'</td>');
}
else if(i%4==0)
{
table.push('<td>'+weather_data[i*3]+'</td></tr>');
}
else
{
table.push('<td>'+weather_data[i*3]+'</td>');
}
}
Which correctly displays the top line, but the rest is all undefined because the called indexes are too large for the array obviously. I have worked out the the 2nd row needs to call the following index: ((index-5)*3)+1, but the 3rd row is going to require a different index call too.. there must be an equation that will work for all rows.. this must be a fairly common operation!
The resultant table needs to show indexes in the following order:
0 3 6 9 12
1 4 7 10 13
2 5 8 11 14
So the loop needs to generate these in order of left to right, then next row etc.
~It also needs to added the necessary starting <tr> and </tr> tags respectively.