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.
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.
The last index is length - 1
, not length
, which is why your first result will be out of bounds and show undefined
.
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
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);
}
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.