Untitled
unknown
plain_text
a year ago
3.6 kB
7
Indexable
const rows = document.querySelectorAll('.row'); // Rows in the crossword grid
const words = document.querySelectorAll('.word'); // Words list
const clues = document.querySelectorAll('.clue'); // Clues list
const matchedWordsWithClues = []; // To store the matched results
// Helper function to extract horizontal words and match with clues
const extractHorizontalWords = () => {
rows.forEach((row, i) => {
let word = [];
const squares = row.querySelectorAll('.square');
squares.forEach((square, j) => {
const input = square.querySelector('.char');
if (input) {
word.push({ input, i, j });
} else {
if (word.length > 1) {
const wordIndex = word[0].input.dataset.wordIndex; // Use a custom dataset for index if necessary
const clue = clues[wordIndex]?.textContent.trim() || '';
matchedWordsWithClues.push({
wordStart: [word[0].i, word[0].j],
direction: 'horizontal',
clue,
});
}
word = [];
}
});
if (word.length > 1) {
const wordIndex = word[0].input.dataset.wordIndex; // Use a custom dataset for index if necessary
const clue = clues[wordIndex]?.textContent.trim() || '';
matchedWordsWithClues.push({
wordStart: [word[0].i, word[0].j],
direction: 'horizontal',
clue,
});
}
});
};
// Helper function to extract vertical words and match with clues
const extractVerticalWords = () => {
const columnCount = Math.max(...Array.from(rows).map(row => row.querySelectorAll('.square').length));
for (let j = 0; j < columnCount; j++) {
let word = [];
rows.forEach((row, i) => {
const squares = row.querySelectorAll('.square');
const square = squares[j];
if (square) {
const input = square.querySelector('.char');
if (input) {
word.push({ input, i, j });
} else {
if (word.length > 1) {
const wordIndex = word[0].input.dataset.wordIndex; // Use a custom dataset for index if necessary
const clue = clues[wordIndex]?.textContent.trim() || '';
matchedWordsWithClues.push({
wordStart: [word[0].i, word[0].j],
direction: 'vertical',
clue,
});
}
word = [];
}
}
});
if (word.length > 1) {
const wordIndex = word[0].input.dataset.wordIndex; // Use a custom dataset for index if necessary
const clue = clues[wordIndex]?.textContent.trim() || '';
matchedWordsWithClues.push({
wordStart: [word[0].i, word[0].j],
direction: 'vertical',
clue,
});
}
}
};
// Extract and match words and clues
extractHorizontalWords();
extractVerticalWords();
// Output the matched results
console.log(matchedWordsWithClues);Editor is loading...
Leave a Comment