therow = new Array (0,3,2,1,9,5,7,6,8,4,11,10)
That's an array of Number.
therow = form.inputbox.value.split(",");
That's an array of String.
You then attempt to do arithmetic on the strings. '2'+'3'='23' not '5', so you get unexpected results.
for (var i= therow.length; i-->0;)
therow[i]= +therow[i]; // convert to Number
Also matrix() seems much more complicated that it needs to be. How about:
function matrix(row) {
var table= document.getElementById('matrixbox');
if (table)
table.parentNode.removeChild(table);
table= document.createElement('table');
table.id= 'matrixbox';
for (var i= 0; i<row.length; i++) {
var tr= table.insertRow(i);
for (var j= 0; j<row.length; j++) {
// this is the actual calculation here
var value= (row[j]-row[i] +row.length)%row.length;
tr.insertCell(j).appendChild(document.createTextNode(value));
}
}
document.getElementById('matrixarea').appendChild(table);
}
Then, to get rid of that nasty therow global, call using:
<body onload="matrix([0,3,2,1,9,5,7,6,8,4,11,10])">
and:
function execute(form) {
var row= form.elements.inputbox.value.split(',');
for (var i= row.length; i-->0;)
row[i]= +row[i];
matrix(row);
}
As a bonus, will work for any row length.
(Forget cellpadding/cellspacing; set ‘padding’ and ‘border-spacing’ through CSS instead. And don't use setAttribute() on HTML elements, as there are bugs in it on IE. And it's dead ugly).
bobince
2009-10-07 11:07:31