Printing diagonal Array

function printLRDiagonal(board) {
  let newArray = [];
  for(let i = 0; i < board.length; i++) {
    for(let j = 0; j < board.length; j++) {
      if(i === j) {
        newArray.push(board[i][j])
      }
    }
  }
  return newArray;
}

let board = [
  ['X', 'O', 'X'],
  ['O', '3', 'X'], 
  ['O', 'X', '7'],
  ['O', 'X', 'w'],
];

console.log(printLRDiagonal(board));

The above code can print a 3x3 diagonal array but it’s not working for a 4X4 array. How do I fix this up?

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

2 Likes

Have you tried it with a 4X4 array? It’s working for me. Check your test data closely.

1 Like

Yes. Its showing undefined

Please show us, the 4X4 array you are trying, what you expect the result to be, and what the result actually is.

[X X X X]
[X D X X]
[X X K X]
[X X X Q]

In the case above, it should print XDKQ

Hi, I did some test with your code.
I don’t see reasons why it should not work.

function printLRDiagonal(board) {
  let newArray = [];
  for(let i = 0; i < board.length; i++) {
    for(let j = 0; j < board.length; j++) {
      if(i === j) {
        newArray.push(board[i][j])
      }
    }
  }
  return newArray;
}


let test = [
  [3, 1, 1, 1],
  [1, 3, 1, 1],
  [1, 1, 3, 1],
  [1, 1, 1, 3]
  ];
  
  
  console.log(printLRDiagonal(test))//[ 3, 3, 3, 3 ]

Assumption: what if you have some syntax issues when declaring test array? I would double check it.

1 Like

That is not valid JS.

Show the exact JS object and the exact string you are expecting and the exact string you are getting.

Again, look closely at the sample data in your original post. Is that a 4X4 array?

1 Like

function printLRDiagonal(board) {

}

let board = [
[‘X’, ‘O’, ‘X’],
[‘O’, ‘O’, ‘X’],
[‘O’, ‘X’, ‘X’]
];

console.log(printLRDiagonal(board));

Expected output should be XOX

For your code to work the length of the last nested array would have to be the same length as the outer array.

i is 0, j is 0
you push board[i][j] // X
i is 1, j is 1
you push board[i][j] // 3
i is 2, j is 2
you push board[i][j] // 7
i is 3, j is 3 (does the last array have an element at index 3?)
you push board[i][j] // undefined
1 Like

Again, I’ve been trying to hint … I think your code is right, you’re just testing it wrong:

function printLRDiagonal(board) {
  let newArray = [];
  for(let i = 0; i < board.length; i++) {
    for(let j = 0; j < board.length; j++) {
      if(i === j) {
        newArray.push(board[i][j])
      }
    }
  }
  return newArray;
}

console.log(printLRDiagonal(
 [
  [1, 2, 3],
  [4, 5, 6],
  [7, 9, 9],
]))
// [1, 5, 9]

console.log(printLRDiagonal(
 [
  [ 1,  2,  3,  4],
  [ 5,  6,  7,  8],
  [ 9, 10, 11, 12],
  [13, 14, 15, 16],
]))
// [1, 6, 11, 16]

As a note, I’d want to test it with unique values in each cell to make sure I don’t get a false pass.

2 Likes

Thank you freecodecamp

How is the code going to be for left diagonal to get this output 4, 7, 10, 13?

Lets try to come up with pattern first.
for 4x4 array:

4 >>> has indexes 0, 3
7 >>> has indexes 1, 2
10 >>> has indexes 2, 1
13 >>> has indexes 3, 0

I see the pattern which can be coded. Do you see it? Try to do the same analysis for the test array 3x3 size.

1 Like

You can edit your post, press ctrl-e, and make your code look like this:

code is better-looking when formatted like this
1 Like

I came up with solution.
Can give you a hint: I changed single line of code from your original post, that was enough

1 Like

function printLRDiagonal(board) {
let newArray = ;
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board.length; j++) {
if (i === j) {
newArray.push(board[j][3]);
}
}
}
return newArray;
}

console.log(
printLRDiagonal([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
])
);

I ended up printing the last element in array

I guess you moved in different direction.

I changed condition for if, not the pushing line

Lets talk about this:

if (i === j) {
newArray.push(board[j][3]);
}

By this you are saying(specific iteration example):
if i === 1 and j===1, add to the result this: board[1][3] >>> which will be 8

if i === 2 and j===2, add to the result this: board[2][3] >>> which will be 12

This logic doesn’t make much sense to me.