using javascript, need create matrix table looks matrix below:
0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0
the following code, me, seems should correct, when try output arr[0][1] etc... no value show. did wrong in following code:
var address = [ "...", "....", ".....", "......" ]; var arr = []; (i = 0; < address.length; i++) { (j = 0; j < address.length; j++) { //alert(i + " " + j); if (i=j) { arr[i][j]=0; } else { arr[i][j]=1; } } }
you have 2 problems in script
for (i = 0; < address.length; i++) { //need initialize arr[i] else `arr[i]` return undefined arr[i]= []; (j = 0; j < address.length; j++) { //need == not = if (i == j) { arr[i][j] = 0; } else { arr[i][j] = 1; } } } console.log(arr)
where if..else
can replaced ternary operator
for (j = 0; j < address.length; j++) { arr[i][j] = == j ? 0 : 1; }
demo: fiddle