1

let bigo = [[1,2,3],[4,5,6],[7,8,9]];

for(let i = bigo.length; i >= 0; i--){
  console.log(bigo[i]);
}

Result:

> undefined
> [ 7, 8, 9 ]
> [ 4, 5, 6 ]
> [ 1, 2, 3 ]

I keep getting undefined before the result show. What am I doing wrong.

RenaudC5
  • 3,553
  • 1
  • 11
  • 29
Kay
  • 59
  • 4

4 Answers4

1

The last index is length - 1, not length, which is why your first result will be out of bounds and show undefined.

Nicky
  • 3,607
  • 6
  • 33
  • 64
0

bigo is 3 items long but does not have anything at index position 3. This is because indices count from 0. You need to write let i = bigo.length - 1

Ben
  • 403
  • 4
  • 16
0

Save yourself the hassle and stop using index-based loops.

let bigo = [[1,2,3],[4,5,6],[7,8,9]];

for (let arr of [...bigo].reverse()) {
  console.log(arr);
}
connexo
  • 53,704
  • 14
  • 91
  • 128
-2

If bigo.length = 9 the programm will ask for bigone[9], after that it will decrease i with 1. I think you messed up the order.

  • 2
    1) ```bigo.length``` is 3, as it is a nested array. 2) Just stating "I think you messed up the order" is nowhere near specific enough for an answer. Be more specific and/or provide an alteration to the code that would solve the OPs issue. – Ben May 23 '22 at 13:22