RosettaCodeData/Task/Identity-matrix/Jsish/identity-matrix.jsish

34 lines
702 B
Plaintext

/* Identity matrix, in Jsish */
function identityMatrix(n) {
var mat = new Array(n).fill(0);
for (var r in mat) {
mat[r] = new Array(n).fill(0);
mat[r][r] = 1;
}
return mat;
}
provide('identityMatrix', 1);
if (Interp.conf('unitTest')) {
; identityMatrix(0);
; identityMatrix(1);
; identityMatrix(2);
; identityMatrix(3);
var mat = identityMatrix(4);
for (var r in mat) puts(mat[r]);
}
/*
=!EXPECTSTART!=
identityMatrix(0) ==> []
identityMatrix(1) ==> [ [ 1 ] ]
identityMatrix(2) ==> [ [ 1, 0 ], [ 0, 1 ] ]
identityMatrix(3) ==> [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ]
[ 1, 0, 0, 0 ]
[ 0, 1, 0, 0 ]
[ 0, 0, 1, 0 ]
[ 0, 0, 0, 1 ]
=!EXPECTEND!=
*/