Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 792165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:57:00+00:00 2026-05-14T21:57:00+00:00

So I found this thread that was extremely helpful in traversing an array diagonally.

  • 0

So I found this thread that was extremely helpful in traversing an array diagonally. I’m stuck though on mirroring it. For example:

var m = 3;
var n = 4;
var a = new Array();
var b = 0;

for(var i = 0; i < m; i++) {
  a[i] = new Array(n);
  for(var j = 0; j < n; j++) {
    a[i][j] = b;
      b++;
  }
}

for (var i = 0; i < m + n - 1; i++) {
  var z1 = (i < n) ? 0 : i - n + 1;
  var z2 = (i < m) ? 0 : i - m + 1;
  for (var j = i - z2; j >= z1; j--) {
    console.log(a[j][i - j]);
  }
}

Console reads [[0],[4,1],[8,5,2],[9,6,3],[10,7],[11]]

I’d like it to read [[8],[4,9],[0,5,10],[1,6,11],[2,7],[3]]

Been stumped for awhile, it’s like a rubik’s cube >_<

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:57:00+00:00Added an answer on May 14, 2026 at 9:57 pm

    Well, I found the whole z1, z2 logic a bit unreadable, so I did it a bit differently:

    var m = 3;
    var n = 4;
    var a = new Array();
    var b = 0;
    
    for(var i = 0; i < m; i++) {
      a[i] = new Array(n);
      for(var j = 0; j < n; j++) {
        a[i][j] = b;
          b++;
      }
    }
    
    var out = new Array();
    for (var i = 1 - m; i < n; i++) {
        var group = new Array();
        for (var j = 0; j < m; j++) {
            if ((i + j) >= 0 && (i + j) < n) {
                group.push(a[j][i + j]);
            }
        }
        out.push(group);
    }
    console.log(out);
    

    Prints [[8], [4, 9], [0, 5, 10], [1, 6, 11], [2, 7], [3]] to the console.

    How it works

    Your matrix construction gives you a rectangle like this (where your a array is the set of rows):

     0  1  2  3
     4  5  6  7
     8  9 10 11
    

    Which means the diagonals are over this grid:

     #  #  0  1  2  3
        #  4  5  6  7  #
           8  9 10 11  #  #
    

    Now we’re just looping over a skewed rectangle, that would look like this normalised:

     #  #  0  1  2  3
     #  4  5  6  7  #
     8  9 10 11  #  #
    

    Now you’ll notice that for each row you add, you end up with an extra column (starting with a #) and that the first column is now skewed by this amount (if you imagine holding the first row in place & sliding the rows below to the left). So for our outer for loop (over the columns), the first column is effectively the old first column, 0, minus the number of rows m, plus 1, which gives 0 - m + 1 or 1 - m. The last column effectively stays in place, so we’re still looping to n. Then its just a matter of taking each column & looping over each of the m rows (inner for loop).

    Of course this leaves you with a bunch of undefineds (the #s in the grid above), but we can skip over them with a simple if to make sure our i & j are within the m & n bounds.

    Probably slightly less efficient than the z1/z1 version since we’re now looping over the redundant # cells rather than pre-calculating them, but it shouldn’t make any real world difference & I think the code ends up much more readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 389k
  • Answers 389k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could always implement the events in global.asax. Implement Application_Start()… May 15, 2026 at 12:36 am
  • Editorial Team
    Editorial Team added an answer You need to have AutoPostBack = True and hook up… May 15, 2026 at 12:36 am
  • Editorial Team
    Editorial Team added an answer Go read this ("Performance Profiling Parse vs. TryParse vs. ConvertTo")… May 15, 2026 at 12:36 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.