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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video