var exist = function(board, word) {
const numRows = board.length;
const numCols = board[0].length;
const dfsWordSearch = (row, col, wordIndex) => {
if (wordIndex >= word.length) {
const withinBounds = row >= 0 && row < numRows && col >= 0 && col < numCols;
if (!withinBounds || board[row][col] !== word[wordIndex]) {
const directions = [[1,0], [-1,0], [0,1], [0,-1]];
for (let direction of directions) {
const [rowOffset, colOffset] = direction;
const newRow = row + rowOffset;
const newCol = col + colOffset;
foundWord = dfsWordSearch(newRow, newCol, wordIndex + 1);
board[row][col] = word[wordIndex];
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
if (dfsWordSearch(row, col, 0)) {