3

I defined an array in javascript like this:

var chessboard = []; 
chessboard.push(["a1", "b1", "c1","d1","e1","f1","g1","h1"]);
chessboard.push(["a2", "b2", "c2","d2","e2", "f2","g2","h2"]);
chessboard.push(["a3", "b3", "c3","d3","e3", "f3","g3","h3"]);
chessboard.push(["a4", "b4", "c4","d4","e4", "f4","g4","h4"]);
chessboard.push(["a5", "b5", "c5","d5","e5", "f5","g5","h5"]);
chessboard.push(["a6", "b6", "c6","d6","e6", "f6","g6","h6"]);
chessboard.push(["a7", "b7", "c7","d7","e7", "f7","g7","h7"]);
chessboard.push(["a8", "b8", "c8","d8","e8", "f8","g8","h8"]);

What I'm struggling to find out is how to find the index if the element is passed.

Example: If I pass "a5" the programme should be able to tell me (row,column) as (4,0)

**CODE:**
<!DOCTYPE html>
<html>
<head>
<title>Javascript Matrix</title>
</head>
<body>
<script>
var chessboard = [];
chessboard.push(["a1", "b1", "c1","d1","e1", "f1","g1","h1"]);
chessboard.push(["a2", "b2", "c2","d2","e2", "f2","g2","h2"]);
chessboard.push(["a3", "b3", "c3","d3","e3", "f3","g3","h3"]);
chessboard.push(["a4", "b4", "c4","d4","e4", "f4","g4","h4"]);
chessboard.push(["a5", "b5", "c5","d5","e5", "f5","g5","h5"]);
chessboard.push(["a6", "b6", "c6","d6","e6", "f6","g6","h6"]);
chessboard.push(["a7", "b7", "c7","d7","e7", "f7","g7","h7"]);
chessboard.push(["a8", "b8", "c8","d8","e8", "f8","g8","h8"]);
alert(chessboard[0][1]); // b1
alert(chessboard[1][0]); // a2
alert(chessboard[3][3]); // d4
alert(chessboard[7][7]); // h8
</script>
</body>
</html>

This is where I am right now.

EDIT2:

Thank you so much everyone :) I feel very happy.

It seems there are multiple ways to it! What I'm trying to do is this >> Find out the (row,column) of two squares. Example: Square 1: a4
Square 2: c7

||x,y|| = row1-row2, column1-column2

Now find out (x,y) from another 8x8 matrix/array. And display data from matrix(x,y).

5
  • Can you please share the code you have tried and failed? Commented Oct 3, 2017 at 9:03
  • Let me update the question Commented Oct 3, 2017 at 9:04
  • I mean.... a5 already tells you what row/column it is in, just by the name. Just translate A to 1, B to 2, etc and you are done Commented Oct 3, 2017 at 9:08
  • What I'm trying to do is this - Make the programme find out row and column of two squares. Example: a4 and e8 would be > (0,3) and (4,7) and use this data to compute something else. Commented Oct 3, 2017 at 9:16
  • Thank you so much everyone :) Very helpful :D Commented Oct 3, 2017 at 9:25

5 Answers 5

3

Since it's a chessboard you can get the info from the element itself, without iterating the board:

function findCoo(el) {
  return [
    el[1] - 1, // the row value - 1
    el[0].codePointAt() - 'a'.codePointAt() // the column ascii value - ascii value of a
  ];
}

console.log("a5", findCoo("a5"));
console.log("d6", findCoo("d6"));
alert("a5" + ' ' + findCoo("a5"));

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

6 Comments

Replaced console.log with alert and got confused. Works :) Interesting idea.
I've added an alert for you :)
Is it possible to store the output of funcoo(x) to two variables for row and column ?
You can't return two variables from a method, but you can achieve that with array destructuring - var [r, c] = findCoo("a5").
thanks :) var [col1,row1] = findCoo("a5"); alert(row1) //0 alert(col1) //4 var [col2,row2] = findCoo("c7"); alert(row2) //2 alert(col2) //6 //so far so good //I wonder how the below code can be made to work. var k.row = row1 - row2; var k.col = col1 - col2; alert(k.row) alert(k.col)
|
3

For frequent use, I suggest to take an object with the positions and return just an array with the coordinates of the wanted field.

var chessboard = [["a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1"], ["a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2"], ["a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3"], ["a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4"], ["a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5"], ["a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6"], ["a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7"], ["a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8"]],
    positions = Object.create(null); // empty object without prototypes

chessboard.forEach(function (a, i) {
    a.forEach(function (b, j) {
        positions[b] = [i, j];
    });
});

console.log(positions['a5']); // [4, 0]
console.log(positions);
.as-console-wrapper { max-height: 100% !important; top: 0; }

For getting a field name, you could use Number#toString, with a radix of 36 for letters.

function getField(i, j) {
    return (j + 10).toString(36) + (i + 1).toString(10);
}

console.log(getField(4, 0)); // 'a5'
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

Thanks :) I have a doubt. What the opposite way of doing this. Example: I have the positions as 4,0 how to get the square name (a5) ?
1

You can try something like this:

Logic:

  • Loop over all array chessBoard and iterate over every row.
  • Check if the value exists in the row.
    • If yes, return row as iterator and column as index

var chessboard = []; 
chessboard.push(["a1", "b1", "c1","d1","e1","f1","g1","h1"]);
chessboard.push(["a2", "b2", "c2","d2","e2", "f2","g2","h2"]);
chessboard.push(["a3", "b3", "c3","d3","e3", "f3","g3","h3"]);
chessboard.push(["a4", "b4", "c4","d4","e4", "f4","g4","h4"]);
chessboard.push(["a5", "b5", "c5","d5","e5", "f5","g5","h5"]);
chessboard.push(["a6", "b6", "c6","d6","e6", "f6","g6","h6"]);
chessboard.push(["a7", "b7", "c7","d7","e7", "f7","g7","h7"]);
chessboard.push(["a8", "b8", "c8","d8","e8", "f8","g8","h8"]);

function findPosition(str){
  for(var i = 0; i< chessboard.length; i++) {
    var index = chessboard[i].indexOf(str);
    if(index>=0) {
      return "row: " + i + ", col: " + index;
    }
  }
}

console.log(findPosition('a5'));
console.log(findPosition('d5'));

Comments

1

Since what you're storing is a chessboard, so instead of traversing all the elements inside the array and do searching, you can add a method to chessboard and return the [row,column] by some simple calculation:

let chessboard = [["a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1"], ["a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2"], ["a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3"], ["a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4"], ["a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5"], ["a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6"], ["a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7"], ["a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8"]]

chessboard.findEl = (input) => ([input[1]-1 ,input[0].charCodeAt(0)-97])

console.log(chessboard.findEl("a5"))
console.log(chessboard.findEl("b4"))

3 Comments

Please check other answers(Ori's) if this is not covered already.
@Rajesh Mine is different than Ori's because (a) mine is adding a member method into chessboard instead of declaring a standalone function to do searching,and (b) what I wrote is an arrow method which gives cleaner syntax. Please check one's answer and do a comparison first before making a judgement.
@Rajesh First, like you said, it's best left for the consumer to implement things so which way the consumer does is beyond you and me. Second, what I gave is only an implementation, I have no gain (unless I'm really fishing for upvotes and whatnot), let alone competing with others. Third, I actually didn't see Ori's answer when I was writing my own, but then again, a good answer is not judged merely by logic. Last but not least, I can think of multiple scenarios where my solution can be a better practice. Personal preference, maybe, but I also maintained my code quality.
0

Try this

function getElement(val){
var result;
  for(var i=0;i<chessboard.length;i++){
         result=chessboard[i].indexOf(val);
     if(result!=-1){
        result='[' + i+',' + result + ']';
        break;
     }
  }
  return result;
}

console.log(getElement("a5"));

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.