Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I traverse an array diagonally in javascript [closed]

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.

like image 752
viperia Avatar asked Mar 10 '16 13:03

viperia


People also ask

How do I traverse an array diagonally?

Example: digonalLeft sums \ of matrix, because (0,0) (1,1) (2,2) makes the diagonal. diagonalRight sums / of matrix, because (0+2) = (1+1) = (2+0) = 2 and 2 is the array.

How do you move the diagonal of a matrix?

Code Explanation:We keep incrementing index as we move to next diagonal. We increment column begin index until it reaches end, as it for the next iterations it will be stopped at last index and we'll be incrementing row begin index moving from this point.

How do I print an array diagonally?

Start from the index (0,0) and print the elements diagonally upward then change the direction, change the column and print diagonally downwards. This cycle continues until the last element is reached. Algorithm: Create variables i=0, j=0 to store the current indices of row and column.

How do you read a diagonal matrix?

Best way to think about this problem is that each cell on a single diagonal must satisfy x+y=d where d is the index of the diagonal(from 0 to n+m-2 inclusive). You can loop over d and for each d loop over x. Using the equation above we get: y=d-x .


1 Answers

From top left to bottom right

var array = ["ABCD","EFGH","IJKL"];    var Ylength = array.length;  var Xlength = array[0].length;  var maxLength = Math.max(Xlength, Ylength);  var temp;  for (var k = 0; k <= 2 * (maxLength - 1); ++k) {      temp = [];      for (var y = Ylength - 1; y >= 0; --y) {          var x = k - y;          if (x >= 0 && x < Xlength) {              temp.push(array[y][x]);          }      }      if(temp.length > 0) {          document.body.innerHTML += temp.join('') + '<br>';      }  }

(see also this Fiddle)


From the bottom left to top right

var array = ["ABCD","EFGH","IJKL"];    var Ylength = array.length;  var Xlength = array[0].length;  var maxLength = Math.max(Xlength, Ylength);  var temp;  for (var k = 0; k <= 2 * (maxLength - 1); ++k) {      temp = [];      for (var y = Ylength - 1; y >= 0; --y) {          var x = k - (Ylength - y);          if (x >= 0 && x < Xlength) {              temp.push(array[y][x]);          }      }      if(temp.length > 0) {          document.body.innerHTML += temp.join('') + '<br>';      }  }

(see also this Fiddle)


Combined

As there's but a single line of difference between both, you can easily combine them in a single function :

var array = ["ABCD","EFGH","IJKL"];    function diagonal(array, bottomToTop) {      var Ylength = array.length;      var Xlength = array[0].length;      var maxLength = Math.max(Xlength, Ylength);      var temp;      var returnArray = [];      for (var k = 0; k <= 2 * (maxLength - 1); ++k) {          temp = [];          for (var y = Ylength - 1; y >= 0; --y) {              var x = k - (bottomToTop ? Ylength - y : y);              if (x >= 0 && x < Xlength) {                  temp.push(array[y][x]);              }          }          if(temp.length > 0) {              returnArray.push(temp.join(''));          }      }      return returnArray;  }    document.body.innerHTML = diagonal(array).join('<br>') +                            '<br><br><br>' +                            diagonal(array, true).join('<br>');

(see also this Fiddle)

like image 75
John Slegers Avatar answered Sep 20 '22 06:09

John Slegers