+ 1
1
10. Write a JavaScript function which returns the n rows by n columns identity matrix.
6 ответов
+ 3
is in put the number n?
+ 2
bro whats input?
+ 2
function identityMatrix(n) {
    var a = [];
    for(i=0;i<n;i++) {
        for(j=0;j<n;j++) {
            a[i][j] = (i == j) ? 1 : 0;
        }
    }
    return a;
}
+ 1
var n = 4;
var newArr = function () {
    var result, l, i;
    if(arguments.length > 0) {
        l = [].slice.call(arguments, 0, 1)[0];
        
        result = new Array(l);
        for(i = 0; i < l; i++) {
            result[i] = newArr.apply(null, [].slice.call(arguments, 1));
        }
    } else {
        return 0;
    }
    return result;
};
function matIdentity(n) {
    var matrix = newArr(n,n)
    
    for (i = 0; i < n; ++i) {
       matrix[i][i] = 1;
    }
    
    return matrix;
}
alert(matIdentity(n))
0
10. Write a JavaScript function which returns the n rows by n columns identity matrix.
0
n     



