Just started working on a basic grid analysis algorithm in JavaScript but I have come up against an error that is perplexing me.
var max = 9; var testArray = new Array( ['7', '3', '9', '6', '4', '1', '5', '2', '8'], ['1', '8', '2', '7', '5', '3', '4', '6', '9'], ['9', '5', '7', '3', '8', '2', '1', '4', '6'], ['3', '1', '4', '9', '6', '7', '2', '8', '5'], ['6', '2', '8', '5', '1', '4', '9', '3', '7'], ['5', '4', '6', '2', '9', '8', '3', '7', '1'], ['8', '7', '1', '4', '3', '5', '6', '9', '2'], ['2', '9', '3', '1', '7', '6', '8', '5', '4'] ); function checkYoSelf(myGrid) { var i; var j; var horizLine = new String; for( i = 0; i <= (max - 1); i++ ) { for( j = 0; j <= (max - 1); j++) { document.write(i+'<br />'); horizLine += myGrid[i][j]; } var test = RegExp(i, 'ig'); var result = new Array(horizLine.match(test)); if( result.length > 1 ) { alert('fail'); } } } html file has <a href='#' onclick='checkYoSelf(testArray);'>check</a>
According to firebug myGrid[i] is undefined but I’m not sure why this should be.
What am I doing wrong?
Well, this is working for me… I’ve just replaced your ‘max’ variable to something more dynamic:
Not sure what you’re trying to do with this, but at least it doesn’t give any error.