1

I am trying to write a jQuery that will find the index of a specific value within a 7x7 2D array.

So if the value I am looking for is 0 then I need the function to search the 2D array and once it finds 0 it stores the index of the two indexes.

This is what I have so far, but it returns "0 0" (the initial values set to the variable.

Here is a jsFiddle and the function I have so far:

http://jsfiddle.net/31pj8ydz/1/

$(document).ready( function() {

    var items = [[1,2,3,4,5,6,7], 
                 [1,2,3,4,5,6,7], 
                 [1,2,3,0,5,6,7], 
                 [1,2,3,4,5,6,7], 
                 [1,2,3,4,5,6,7], 
                 [1,2,3,4,5,6,7], 
                 [1,2,3,4,5,6,7]];

    var row = 0;
    var line = 0;

    for (i = 0; i < 7; ++i) {
        for (j = 0; i < 7; ++i) {
            if (items[i, j] == '0,') {
                row = i;
                line = j;
            }
        }
    }

    $('.text').text(row + ' ' + line);
});

HTML:

<p class="text"></p>
4
  • multidimension indexers in javascript are [][], ie items[i][j] Commented Jan 13, 2015 at 12:56
  • i think the correct terms are row and collumn instead of row and line (line == row) Commented Jan 13, 2015 at 12:57
  • You're also not incrementing or checking j in the inner for loop. Commented Jan 13, 2015 at 12:57
  • updated my answer, with some details. Commented Jan 13, 2015 at 13:13

7 Answers 7

1

Your if statement is comparing

if (items[i, j] == '0,')

Accessing is wrong, you should use [i][j].

And your array has values:

[1,2,3,4,5,6,7]
....

Your value '0,' is a string, which will not match numeric values inside the array, meaning that your row and line won't change.

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

Comments

1

First, you are accessing your array wrong. To access a 2D array, you use the format items[i][j].

Second, your array doesn't contain the value '0'. It doesn't contain any strings. So the row and line variables are never changed.

You should change your if statement to look like this:

if(items[i][j] == 0) {

Notice it is searching for the number 0, not the string '0'.

3 Comments

actually, it contains the value '0'
@andre On second look, it does contain the number 0, but it doesn't contain the string '0', which is what the OP was searching for. I fixed my answer.
actually, the comparison operator used allow to compare a string against an integer, unless the comparison operator was ===
1

You access your array with the wrong way. Please just try this one:

items[i][j]

When we have a multidimensional array we access the an element of the array, using array[firstDimensionIndex][secondDimensionIndex]...[nthDimensionIndex].

That being said, you should change the condition in your if statement:

 if( items[i][j] === 0 )

Please notice that I have removed the , you had after 0. It isn't needed. Also I have removed the ''. We don't need them also.

Comments

0

There are following problems in the code

1) items[i,j] should be items[i][j].

2) You are comparing it with '0,' it should be 0 or '0', if you are not concerned about type.

3) In your inner for loop you should be incrementing j and testing j as exit condition.

Change your for loop like bellow and it will work

for (i = 0; i < 7; i++) {
        for (j = 0; j < 7; j++) {
            if (items[i][j] == '0') {
                row = i;
                line = j;
            }
        }
    }

DEMO

Note:-

1) Better to use === at the place of ==, it checks for type also. As you see with 0=='0' gives true.

2) Better to say i < items.length and j<items[i].length instead of hard-coding it as 7.

1 Comment

Thanks !! I tried fixing all the errors in my code but the page became completely unresponsive. It was the change of ++i, ++j to i++, j++ that fixed that
0
var foo;
items.forEach(function(arr, i) {
    arr.forEach(function(val, j) {
        if (!val) { //0 coerces to false
            foo = [i, j];
        }
    }
}

Here foo will be the last instance of 0 in the 2D array.

Comments

0

You are doing loop wrong On place of

 for (i = 0; i < 7; ++i) {
        for (j = 0; i < 7; ++i) {
            if (items[i, j] == '0,') {
                row = i;
                line = j;
            }
        }
    }

use this

 for (i = 0; i < 7; i++) {
        for (j = 0; j < 7; j++) {
            if (items[i][j] == 0) {
                row = i;
                line = j;
            }
        }
    }

Here is the demo

Comments

0

Looks like you are still learning how to program. But here is an algorithm I've made. Analyze it and compare to your code ;)

var itens = [[1,2,3,4,5,6,7], 
             [1,2,3,4,5,6,7], 
             [1,2,3,0,5,6,7], 
             [1,2,3,4,5,6,7], 
             [1,2,3,4,5,6,7], 
             [1,2,3,4,5,6,7], 
             [1,2,3,4,5,6,7]];

var row = null;
var column = null;

for (var i = 0; i < itens.length; i++) {
    for (var j = 0; j < itens[i].length; j++) {
        if (itens[i][j] == 0) {
            row = i;
            column = j;
        }
    }
}

console.log(row, column);

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.