I have an array with strings that I would like to traverse diagonally.
Assumptions:
- Each string is the same length.
- Arrays could be square or rectangular, horizontally or vertically.
The matrix looks like this:
A B C D
E F G H
I J K L
I Would like to get (from top left to bottom right):
A
EB
IFC
JGD
KH
L
and (from the bottom left to top right):
I
JE
KFA
LGB
HC
D
I already have a piece of code that works 3/4 of the way, but i cant seem to figure out what I am doing (wrong).
//the array
var TheArray = ['ABCD','EFGH','IJKL'];
//amount of rows
var RowLength = TheArray.length;
//amount of colums
var ColumnLength = TheArray[0].length;
The code I have chops up the diagonals into 4 of these loops to get all the diagonals. It looks as 2 for loops with an if to not loop over unbound values. The pseudo code looks a bit like this:
for(loop rows){
var outputarray = [];
for(loop columns){
if(delimit for out of bound){
var temprow = TheArray[something?];
var tempvalue = temprow[something?];
outputarray.push(tempvalue);
}
}
//use values
document.getElementById("theDiv").innerHTML += outputarray.join("")+"<br>";
}
I hope somebody can help me with this.