1

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?

3
  • 1
    I would like to propose to you i < max as opposed to i <= (max - 1). Commented Mar 25, 2009 at 22:29
  • Also, just use var horizLine = "". No need for new String. Same with arrays. arr = [ [1,2,3], [4,5,6], [7,8,9] ]; Commented Mar 25, 2009 at 22:37
  • To concatenate array elements into a string, just use Array.join(''). Commented Mar 25, 2009 at 22:42

7 Answers 7

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
1

where is max coming from?

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

you only have 8 rows.

2 Comments

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

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

Comments

0

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++ ) {

2 Comments

max - 2? why not just set max to be the actual max?
@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.
0

Firebug says

max is not defined

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

Comments

0

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.

Comments

0

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");
    }    
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.