Doen Doen - 2 months ago 6
Javascript Question

Initializing the 2D array elements in javacsript

I want to initialize and then print the eleemnts of a 2D array using javascript.
I created this code but nothing displays as output. Anyone help me to output this array? Thanks

var m = 6;
var n = 3;
var mat = new Array[m][n];

for (i = 0; i < mat.length; i++) {
for (j = 0; j < mat[i].length; j++) {
mat[i][j]) = i * j;
document.writeln(mat[i][j]);
}
document.writeln("<br />");
}

Answer

<html>
    <body>
    </body>
    <script>
        var m=6;
        var n=3;
        var mat =new Array(m);

        for( var i=0; i < m ; i++){

            mat[i] = new Array(n);

            for( var j=0;j< n ;j++){
                mat[i][j] = i*j;
                document.writeln(mat[i][j]);
            }

            document.writeln("<br />");
        }
    </script>
</html>