最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nesting for loop multidimensional array. Conceptual - Stack Overflow

programmeradmin3浏览0评论

I've been working on some exercises on freecodecamp and I am stuck on this nesting for loop exercise. I was able to find the solution, but I don't quite understand.

Can someone explain to me how the second loop with the variable J works? I have read online saying the first for loop is for the outer array and the second one is the inner array, but why stop at two for loops, why not three?

function multiplyAll(arr) { 
    var product = 1;

    // Only change code below this line

    for (var i=0;i<arr.length;i++) {
        for (var j=0;j<arr[i].length;j++) {
            product *= arr[i][j];
        }
    }

    // Only change code above this line
    return product;
}

// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);

I've been working on some exercises on freecodecamp and I am stuck on this nesting for loop exercise. I was able to find the solution, but I don't quite understand.

Can someone explain to me how the second loop with the variable J works? I have read online saying the first for loop is for the outer array and the second one is the inner array, but why stop at two for loops, why not three?

function multiplyAll(arr) { 
    var product = 1;

    // Only change code below this line

    for (var i=0;i<arr.length;i++) {
        for (var j=0;j<arr[i].length;j++) {
            product *= arr[i][j];
        }
    }

    // Only change code above this line
    return product;
}

// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
Share Improve this question edited Mar 3, 2016 at 8:50 morcen 3682 silver badges14 bronze badges asked Mar 3, 2016 at 7:16 StevStev 411 silver badge3 bronze badges 3
  • Because inner array has length = 2..and looping condition is less than length so it will iterate for index => 0...1...break... – Rayon Commented Mar 3, 2016 at 7:22
  • 1 You can use a debugger for line by line jump and noting down the values of i and j for every iteration, or I would suggest doing it with pen or paper by yourself for once. – Abhinav Gauniyal Commented Mar 3, 2016 at 7:23
  • Because the array is nested to a level of two. – user663031 Commented Apr 9, 2016 at 11:33
Add a ment  | 

1 Answer 1

Reset to default 7

This is very basic logical questions. The ideal way to understand this is the way @Abhinav mentioned in the ment.

// Module that multiplies all the number in 2D array and returns the product
function multiplyAll(arr) { 
   var product = 1;

    // Iterates the outer array
    for (var i=0;i<arr.length;i++) {
        // 1st Iter: [1,2]
        // 2nd Itr: [3,4]
        // 3rd Itr: [5,6,7]
        console.log('i: ' + i, arr[i]);
        for (var j=0;j<arr[i].length;j++) {
            // Outer loop 1st inner 1st : 1
            // Outer loop 1st inner 2nd : 2
            // Outer loop 2nd inner 1st : 3
            // ...
            console.log('j: ' + j, arr[i][j]);

            // Save the multiplication result

            // Prev multiplication result * current no;
            // Outer loop 1st inner 1st : 1 * 1 = 1 
            // Outer loop 1st inner 2nd : 1 * 2 = 2
            // Outer loop 2nd inner 1st : 2 * 3 = 6
            // Outer loop 2nd inner 1st : 6 * 4 = 24
            // ...
            product *= arr[i][j];
       }
    }

    // Only change code above this line
    return product;
}


multiplyAll([[1,2],[3,4],[5,6,7]]);

Run this in browser console. This might give you some clarity.

发布评论

评论列表(0)

  1. 暂无评论