We lack some context here so must go on some assumptions. First, that puzzle
is iterable since it has a length; and second, that each row in puzzle
is also iterable since it has an index.
The code in line 4 above is polling the value in row i
at column columnIndex
and appending it to the column
array.
Addendum
Arrays have four methods to add or remove the first or last element.
array.push(object)
array.pop()
Using push(), the object is appended to the end of the array. The object can be any JS object, a number, character (or string), another array, an object, or a reference to an object elsewhere in the namespace.
Using pop() removes the last element. We can assign the value in the same process.
array.unshift(object)
array.shift()
The above methods do the same as the first two, respectively, only at the start of the list, not the end.
Iterating an array with two dimensions (as an array of arrays is given to have) requires two iterators, one for the rows, and one for the columns. (for
is an iterator.)
for (row of array) {
for (col of row) {
}
}
Using the ES6 of
operator we poll the values directly, without an index. We iterate over one row at a time, and then iterate over each value from left to right.
For this example I’m going to use a PlayFour puzzle by John Wilmes
JEER ... TAUNT
ORCA ... LARGE WHALE
LION ... LARGE CAT
TENT ... PUP(?POLE)
|||. ... TIRADE
||._ ... SUPPLY AND DEMAND SUBJ.
|.__ ... GREAT LAKE
.___ ... SUDDEN SHOCK
Your for loop is able to recover the vertical words, using the column
array.
const horz = ['JEER', 'ORCA', 'LION', 'TENT'];
const vert = [[], [], [], []];
for (col = 0; col < vert.length; col++) {
for (row = 0; row < horz[col].length; row++) {
vert[col].push(horz[row][col])
}
vert[col] = vert[col].join('');
}
vert.forEach(x => console.log(x));
JOLT
ERIE
ECON
RANT