0

I have the following code:

const BOARD = { X: 3, Y: 5 };

const board = new Array(BOARD.X)
    .fill(0)
    .map((_, idx) =>
        new Array(BOARD.Y).fill().map((elm, idx2) => (elm = `${idx},${idx2}`))
    );

console.log(board)

And is printing in the console:

[
  [ '0,0', '0,1', '0,2', '0,3', '0,4' ],
  [ '1,0', '1,1', '1,2', '1,3', '1,4' ],
  [ '2,0', '2,1', '2,2', '2,3', '2,4' ]
]

How can I invert the order of 'X' row, so it prints:

[
  [ '2,0', '2,1', '2,2', '2,3', '2,4' ],
  [ '1,0', '1,1', '1,2', '1,3', '1,4' ]
  [ '0,0', '0,1', '0,2', '0,3', '0,4' ]
]

2 Answers 2

2

SIMPLY with reverse method

board.reverse()

const BOARD = { X: 3, Y: 5 };

const board = new Array(BOARD.X)
    .fill(0)
    .map((_, idx) =>
        new Array(BOARD.Y).fill().map((elm, idx2) => (elm = `${idx},${idx2}`))
    );
board.reverse();
console.log(board)

Sign up to request clarification or add additional context in comments.

Comments

0

Or generate it with BOARD.X - idx - 1 for the x index.

const BOARD = {
  X: 3,
  Y: 5
};

const board = new Array(BOARD.X)
  .fill(0)
  .map((_, idx) =>
    new Array(BOARD.Y).fill().map((elm, idx2) => (elm = `${BOARD.X-idx-1},${idx2}`))
  );

console.log(board)
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.