I'm not quite sure why is this happening but here's the thing:
I'm trying to code the Longest common sub sequence problem but I'm getting this bug, where I try to add a new value to the matrix (NxM multidimension array) but the value is added to all the arrays in the same position.
Example: Adding 1 to matrx[1][1]
0100000
0100000
0100000
...
0100000
Here's the code
var input="XMJYAUZ;MZJAWXU";
var string=input.split(";");
var matrix=[];
for(var i=0;i<string[0].length+1;i++){
matrix[i]=0;
}
var matrx=[];
for(var j=0;j<string[1].length+1;j++){
matrx[j]=matrix;
}
matrx=buildMatrix(string[0][0],string[1][0],1,1);
function buildMatrix(A,B,x,y){
if (A != B){
matrx[x][y]=Math.max(matrx[x-1][y],matrx[x][y-1]);
}
else{
matrx[x][y]=matrx[x-1][y-1]+1;
}
//check x and y
if(x==string[0].length && y==string[1].length){
}
else if(y==string[1].length){
buildMatrix(string[0][x],string[1][0],x+1,1);
}
else{
buildMatrix(string[0][0],string[1][y],x,y+1);
}
return matrx;
}