1
var numRookCaptures = function (board) {2
const r = board.length;3
const c = board[0].length;11
]; // all the 4 possible directions14
// finding the rook's position15
for (let i = 0; i < r; i++) {16
for (let j = 0; j < c; j++) {17
if (board[i][j] === "R") {24
// traversing all the 4 directions25
for (let [x, y] of dir) {26
res += helper(x, y, rook[0], rook[1]);31
function helper(x, y, i, j) {35
while (newX < r && newY < c && newX >= 0 && newY >= 0) {36
if (board[newX][newY] === "p") {38
break; // break because Rook can't attack more than one pawn in same direction39
} else if (board[newX][newY] === "B") {42
// keep moving towards the same direction