I am having trouble this problem regarding 2d arrays. We must create a javascript function addArrays(al, a2) which takes in two 2d arrays of numbers, a1 and a2 and returns their "sum" in the sense of the matrix sum. If If a is array with the sum, then, for all rows i and all columns j, a[i][j] = a1[i][j] + a2[i][j].
I am completely stumped I am pretty sure my logic to find the sum is right but I don't know if anything is actually stored in the sum array or what this error means. Any help would be greatly appreciated.
This is the code I have so far:
var sum = new Array(a1.length);
for (var k = 0; k<sum.length; k++) {
sum[k] = new Array(sum.length);
}
for (var l = 0; l<sum.length; l++) {
for (var m = 0; m<sum.length[l]; m++) {
sum[l][m].push(a1[l][m] + a2[l][m]);
}
}
return sum;
We are given a test file:
function start() {
var ar1 = [[1,2], [3,4]],
ar11 = [[1,2], [3,4], [5,6]],
ar12 = [[1,2,3], [3,4]],
ar2 = [[6,7], [8,9]],
ar21 = [[6,7], [8,9], [19,11]],
ar22 = [[6,7], [8,9,10]],
ar;
try {
alert( addArrays(ar1, ar2).toSource() );
}
catch (e) {
alert( e );
}}
When I run the program I keep getting the error: TypeError: addArrays(...).toSource is not a function
toSourceis non-standard and doesn't work in your browser. Tryconsole.log(addArrays(ar1, ar2)).toSource()is a non-standard function that is not equally supported by browsers. MDN reference. According to the spec - Firefox appears to be the only browser with confirmed support.