It's important understand how it works <Nesting for loops>

It's important understand how it works <Nesting for loops>
0

#1

I tried this exercises in many ways, i did a search in google and saw many answer… and some help me a lot but i still with doubts!

  1. In this page i can try all the exercises if someone need see the results of the exercises for understand better i leave here: https://repl.it/repls/BetterAthleticWeb
  2. This is the exercise:
    var arr = [
    [1,2], [3,4], [5,6]
    ];
    for (var i=0; i < arr.length; i++) {
    for (var j=0; j < arr[i].length; j++) {
    console.log(arr[i][j]);
    }
    }

I need if someone can explain me why if the var i=0 my output is 123456 and when i change to var i = 1 my output is 3456… Thank you a lot


#2

Hey @veroca88,
See, this your arr:

[[1,2],[3,4],[5,6]]

So, when i=0, the first loop in your code starts from first element of the array because arrays are zero-indexed and thus output includes[1,2].
However, when i=1, the loop begins with second element of the array.
And thus, output does not include [1,2].
Hope this answers your question.


#3


#4

:slight_smile: Thank you!! I got it


#5

Always welcome.
Glad i could help.