DEV Community

Cover image for JavaScript - Diagonal Sum of Matrix (Multidimensional Array )
swapnanilWebDeveloper
swapnanilWebDeveloper

Posted on

JavaScript - Diagonal Sum of Matrix (Multidimensional Array )

JavaScript Map function Regarding Array of objects...

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        var arr = [
           [5  ,4  ,7  ,2  ,3  ],
           [3  ,7  ,5  ,8  ,2  ],
           [1  ,2  ,1  ,3  ,4  ],
           [5  ,1  ,4  ,2  ,6  ],
           [8  ,1  ,3  ,2  ,8  ]
        ];

        var sum = 0;

        document.write("The length of the array is = "+arr.length+"<br>");

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

        document.write("The sumation is = "+sum);
    </script>
</head>
<body>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

expected output is :

  The length of the array is = 5
  The sumation is = 23 

Enter fullscreen mode Exit fullscreen mode

Top comments (0)