Show HN: Find the Fox
wordnerd.co
Show HN: Find the Fox
1–3 of 3 posts
Re: Show HN: Find the Fox
#2function searchWord(grid, word) { const rows = grid.length; const cols = grid[0].length; const wordLength = word.length; let found = false;
const directions = [
{ dr: 0, dc: 1 },
{ dr: 1, dc: 0 },
{ dr: 1, dc: 1 },
{ dr: 1, dc: -1 },
{ dr: 0, dc: -1 },
{ dr: -1, dc: 0 },
{ dr: -1, dc: -1 },
{ dr: -1, dc: 1 }
];
for (let r = 0; r = rows || nc = cols || grid[nr][nc].textContent !== word[k]) {
match = false;
break;
}
positions.push(grid[nr][nc]);
}
if (match) {
positions.forEach(cell => cell.style.backgroundColor = 'pink');
console.log(`Found the word '${word}' at positions:`);
positions.forEach((cell) => {
const index = Array.prototype.indexOf.call(cell.parentElement.children, cell);
const row = Math.floor(index / cols);
const col = index % cols;
console.log(`(${row}, ${col})`);
});
found = true;
return;
}
}
}
}
if (!found) {
console.log(`The word '${word}' was not found in the grid.`);
}
}const gridContainer = document.querySelector('.grid.mb-30'); const gridElements = gridContainer.querySelectorAll('.letter'); const grid = []; const rows = 100; const cols = 10;
for (let i = 0; i searchWord(grid, 'FOX');
Re: Show HN: Find the Fox
#3A little function to help ;) function searchWord(grid, word) { const rows = grid.length; const cols = grid[0].length; const wordLength = word.length; let found = false; const directions = [ { dr: 0, dc: 1 }, { dr: 1, dc: 0 }, { dr: 1, dc: 1 }, { dr: 1, dc: -1 }, { dr: 0, dc: -1 }, { dr: -1, dc: 0 }, { dr: -1, dc: -1 }, { dr: -1, dc: 1 } ]; for (let r = 0; r = rows || nc = cols || grid[nr][nc].textContent !== word[k])…