OK, I’ll admit it … I’ve come up with a new form of senility.
I am trying to create a 2-dimensional square array and turn it into an ‘identy’ matrix.
The example display in the console.log show the correct formation of a zero-ed array,
but when I try to create the identity version it does not compute correctly.
Could someone advise me as to where I am losing it?
The ternary instruction should put a ‘1’ along the diagonal of the blank array.
Here is the code I’m using:
“<!DOCTYPE html><html lang=”en”><head><title> Test Page </title>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width-device-width,initial-scale=1.0, user-scalable=yes”/>
</head><body>
<script>
console.clear();
function sqArray(r=2) {
var c = Array(r).fill(0);
var arr = []; while(arr.push(c) < r);
return arr;
}
function mI (rc) { // identity matrix
var Ident = sqArray(rc);
var r, c, a;
for (r=0; r<rc; r++) {
for (c=0; c<rc; c++) {
a = (r === c) ? 1 : 0;
Ident[r][c] = a;
}
} return Ident;
}
var tst = sqArray(); console.log(JSON.stringify(tst), ‘tt 2×2 Default’);
var msq = sqArray(3); console.log(JSON.stringify(msq), ‘ 3×3 zero filled’);
console.log();
var Ident = mI(3); console.log(JSON.stringify(Ident), ‘ 3×3 identity ???NFG???’);
console.log(JSON.stringify([ [1,0,0], [0,1,0], [0,0,1] ]),’ Expected identity’);
</script>
</body></html>
“