tags:

views:

613

answers:

7

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?

+1  A: 

where is max coming from?

max is defined globally (in this case max = 9)

you only have 8 rows.

nickf
max is defined globally (in this case max = 9)
Nick
@Nick: There are only 8 outer arrays.
Chetan Sastry
A: 

From your comment to nickf's answer: it's because max is 9, but you only have 8 rows in the array.

chaos
A: 

Firebug says

max is not defined

You're trying to access testArray[8] inside checkYoSelf(testArray) when the last index is testArray[7].

Russ Cam
A: 

testArray contains 8 elements. You're accessing testArray[8], which is invalid because the index must be 0..7 (0..8-1). Add another row to your array, or rewrite the first for as:

for( i = 0; i <= (max - 2); i++ ) {
strager
max - 2? why not just set max to be the actual max?
nickf
@nickf, I don't know. I'm working off the code I'm given. If you set the actual max (the variable), you'll be changing other parts of the code as well.
strager
+2  A: 

Well, this is working for me... I've just replaced your "max" variable to something more dynamic:

<script type="text/javascript">
    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;

     var maxRows = myGrid.length;
     for( i = 0; i < maxRows; i++ ) {
      var maxColumns = myGrid[i].length;
      for( j = 0; j < maxColumns; 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");
      }
     }
    }
</script>

<a href='#' onclick="checkYoSelf(testArray);">check</a>

Not sure what you're trying to do with this, but at least it doesn't give any error.

Seb
interesting, your version doesn't throw any errors however when I separate the javascript into a separate js file and source in the html head the same error that I mentioned earlier occurs. Clearly I need to work a lot harder at my javascript!
Nick
A: 

If you rewrite

for( i = 0; i <= (max - 1); i++ ) {
   for( j = 0; j <= (max - 1); j++) {

as

for( i = 0; i < (max - 1); i++ ) {
   for( j = 0; j < max; j++) {

it should work. The first dimension of your grid is only 8.

Andy Mikula
A: 

thanks to everyone for their help, the firebug error is no longer occuring now that the code is:

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'],
  ['5', '4', '6', '2', '9', '8', '3', '7', '1'],
  ['6', '2', '8', '5', '1', '4', '9', '3', '7'],
  ['8', '7', '1', '4', '3', '5', '6', '9', '2'],
  ['2', '9', '3', '1', '7', '6', '8', '5', '4'],
  ['2', '9', '3', '1', '7', '6', '8', '5', '4']
);

function checkYoSelf(myGrid) {
  var i;
  var j;
  var horizLine = new String;
  var max = myGrid.length;

  for( i = 0; i < max; i++ ) {
    for( j = 0; j < max; j++) {
      horizLine += myGrid[i][j];
    }
    var test = RegExp(i, "ig");
    var result = new Array(horizLine.match(test));
    if( result.length > 1 ) {
      alert("fail");
    }    
  }
}
Nick