1

I have this array 8x8 array which is supposed to represent a checkerboard.

 var board = [['RS', 'H',  'RS', 'H',  'RS', 'H',  'RS', 'H'],
          ['BS', 'RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS'],
          ['RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS', 'BS'],
          ['BS', 'RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS'],
          ['RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS', 'BS'],
          ['BS', 'RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS'],
          ['RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS', 'BS'],
          ['F', ' RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS']
 ];

I want to get the element F's row and column number I am trying to use a 4 loop

 function getF(board) {
    var i, j;
    for (i = 0; i < 8; i++) {
      for (j = 0; j < 8; j++) {
        if(board[i][j] == 'F'{

     return {row:1, col:j};

How do I do this to get the indexes of all H elements?

3
  • you need some array to store the result, each element is an object like {row: x, col: y} Commented Oct 14, 2014 at 17:48
  • Your question is unclear, do you want to find the location of F or are you trying to locate all H entries? Commented Oct 14, 2014 at 17:49
  • did you even try your own function? Commented Oct 14, 2014 at 17:50

4 Answers 4

1

You can simplify your function like:

 function getCharIndexes(board, char) {
    var i, j, collection = [];
    for (i = 0; i < board.length; i++) {
      for (j = 0; j < board[i].length; j++) {
        if(board[i][j] == char) {
            collection.push({row:1, col:j}); // should be {row: i, col: j}
        }
      }
    }
    return collection;
  }

To get 'F' call: getCharIndexes(board, 'F');

To get 'H' call: getCharIndexes(board, 'H');

etc.

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

4 Comments

This will not return what you want, since it is returning a position object with a fixed row of 1 for every match of the char parameter. It should be collection.push({row:i, col:j}) instead
@RafaelEyng yes, I doubt it's the problem of copying the OP's code, 1 here should be i.
@RafaelEyng Hi, yes I just copy from OP's code, but now I added comment also.
How can I compare the rows and columns from H and F?
0

First, you are missing your closures for your if statement and for loops, I assume that this is a copy/paste issue.

Next, I don't think your getF function is working, because you are returning the column as j, but the row is always 1. To fix that, just return { row: i, col: j } instead.

Finally, the you can use the same loop operation to get all the 'H's, but just keep them in an array, and as you find them, append them using something similar to: foo.push({ row: i, col: j });

Comments

0

The other answers are almost right:

var board = [['RS', 'H',  'RS', 'H',  'RS', 'H',  'RS', 'H'],
    ['BS', 'RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS'],
    ['RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS', 'BS'],
    ['BS', 'RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS'],
    ['RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS', 'BS'],
    ['BS', 'RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS'],
    ['RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS', 'BS'],
    ['F', ' RS', 'BS', 'RS', 'BS', 'RS', 'BS', 'RS']];

function getChar(board, c) {
    var i, j, collection = [];
    for (i = 0; i < board.length; i++) {
      for (j = 0; j < board[i].length; j++) {
        if(board[i][j] == c)
            collection.push({row:i, col:j});
      }
    }
    return collection;
}

console.log(getChar(board, "F"));
console.log(getChar(board, "H"));

2 Comments

How can I compare the rows and columns from H and F? This way I can see if the H should be a specific distance away from the F? For example it must be one row up and one row to the left of the fox?
@GeorgeCUlloa F is always in 1 position?
0

There are some syntax errors in your code. Besides that, this will do:

 function getIndexesOf(matrix, obj) {
    var i, j;
    var ret = []
    for (i = 0; i < matrix.length; i++) {
      var line = matrix[i];
      for (j = 0; j < line.length; j++) {
        if(board[i][j] == obj) {
          ret.push({row: i, col: j})
        }
      }
    }
    return ret;
  }

This way, you can call it passing what object you want to find (as your 'H'):

getIndexesOf(board, 'H');

Note that:

  1. you are using literal values in your for loops. It would be better to use the length of the matrix and of the lines that you pass in.

  2. in your example you are returning {row:1, col:j}. That is not correct, not even for your purposes in the example. In that case you would return {row:i, col:j}, because i holds the row number, and doesn't make sense to return a fixed row number of 1.

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.