Here is an array containing the letters of the alphabet: ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
From the server, I receive the number of columns to display. Let's assume it's 3 for this example.
I have a function that works well, except when my index matches the array length. In that case, the order gets mixed up and I am unsure how to resolve it.
var i;
var j;
var columns = window.columns; // Assuming 3 for this example.
var lettersArray= ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var rows = Math.ceil(lettersArray.length/columns);
Current progress:
for (i = 0; i < rows; i++)
{
for (j=0; j < lettersArray.length; j=j+rows)
{
var index = i+j;
if (index>=lettersArray.length)
{
break;
}
else
{
PrintArray(lettersArray, index);
}
}
}
The PrintArray function takes the index and the array post-sorting, producing the expected result:
A J S
B K T
C L U
D M V
E N W
F O X
G P Y
H Q Z
I R
However, during execution, the output strays from the intended format:
A J S
B K T
C L U
D M V
I N W
F O X
G P Y
H Q R
E Z