Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This will do it:</p> <pre><code>var arr = []; var rows = 4; var cols = 10; for(var i = 0; i &lt; rows + cols - 1; i++){ for(var j = Math.min(rows, i + 1) - 1; j &gt;= Math.max(0, i - cols + 1); j--){ arr.push((j * cols) + i - j); } } </code></pre> <p>fiddle: <a href="http://jsfiddle.net/BmXpy/" rel="nofollow">http://jsfiddle.net/BmXpy/</a></p> <p>EDIT: Here's my attempt at explaining how I came up with this. IMPORTANT, use the table of numbers above to visualize and if needed, print it and draw out the diagonals.</p> <p>First, think about what we want, it's basically diagonals. In the example above the first diagonal is 0, then 10, 1, then 20, 11, 2, then 30, 21, 12, 3, etc. Now if you think about how many of those diagonals there are, it is <code>rows + cols - 1</code>. That is where we get the first loop:</p> <pre><code>for(var i = 0; i &lt; rows + cols - 1; i++){ </code></pre> <p>Now, ignore for a second the boundries. In the general case (the whole center), each of these diagonals is "rows" long. And since we want to go bottom up, we want a reverse loop. That would look like this for the inner loop:</p> <pre><code>for(var j = rows - 1; j &gt;= 0; j--){ </code></pre> <p>Now, we must deal with both boundries (left and right). </p> <p>For the left boundry, if we look at the number of diagonals which are less than "rows" long, we will see that it is <code>rows - 1</code>. And for these diagonals we'll see that the length of each is <code>i + 1</code>. The following inner loop will handle the general case and the left boundry:</p> <pre><code>for(var j = Math.min(rows, i + 1) - 1; j &gt;= 0; j--){ </code></pre> <p>You will see that for diagonal 0, this will run once, for 1 it will run twice, etc. And for the general case (<code>i &gt;= rows</code>) it will run "rows" times.</p> <p>Now, the right boundry. If we look at which diagonals on the right are shorter than "rows", we will see it is all diagonals greater than "cols" (in the example where cols is 10, 0 indexed, that is row 10 and beyond). Replacing <code>j &gt;= 0</code> with <code>j &gt;= Math.max(0, i - cols + 1)</code> will run to 0 for the general case and the left boundry but shorten for the right boundry. We get this:</p> <pre><code>for(var j = Math.min(rows, i + 1) - 1; j &gt;= Math.max(0, i - cols + 1); j--){ </code></pre> <p>And finally, the actual calculation of the number in each location. <code>i</code> represents the diagonal and <code>j</code> represents the index of the number on the diagonal <code>j = 0</code> is the top number if you're looking at the posted table of numbers. For <code>j = 0</code> (top row of numbers) the number is simply <code>i</code>, for each row below the top, we need to multiply the number by "cols" in order to get the number directly below the first row number, then the number needs to be adjusted to the left. This is done by subtracting <code>j</code> which is the row number. So for <code>j = 1</code> (the 2nd row) we need to move the number left by one (subtract 1). So we have <code>i</code> for the horizontal position on the first row, <code>+ (j * cols)</code> to move it down to the appropriate row and then -j to adjust it to diagonal (if you have drawn the diagonals, trace this out for one of them to get a good visual). We get this:</p> <pre><code>(j * cols) + i - j </code></pre> <p>Put it all together and you get my final code above. Hope that made some sense.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload