Finished
88 * @param {string[]} grid
99 */
1010 constructor(grid) {
11 throw new Error('Remove this line and implement the function');
11 this.grid = grid.map(row => row.toUpperCase());
12 this.rows = this.grid.length;
13 this.cols = this.rows > 0 ? this.grid[0].length : 0;
1214 }
1315
1416 /**
1618 * @returns {{start: number[], end: number[]}|undefined}
1719 */
1820 find(word) {
19 throw new Error('Remove this line and implement the function');
21 // Edge Case: Empty word
22 if (!word || word.length === 0) {
23 return undefined;
24 }
25
26 // Edge Case: Empty grid
27 if (this.rows === 0 || this.cols === 0) {
28 return undefined;
29 }
30
31 const searchWord = word.toUpperCase();
32
33 // Edge Case: Word longer than grid dimensions
34 if (searchWord.length > this.rows && searchWord.length > this.cols) {
35 return undefined;
36 }
37
38 // Define all 8 directions: right, left, down, up, down-right, down-left, up-right, up-left
39 const directions = [
40 [0, 1], // right
41 [0, -1], // left
42 [1, 0], // down
43 [-1, 0], // up
44 [1, 1], // down-right
45 [1, -1], // down-left
46 [-1, 1], // up-right
47 [-1, -1] // up-left
48 ];
49
50 // Try each cell as a starting point
51 for (let row = 0; row < this.rows; row++) {
52 for (let col = 0; col < this.cols; col++) {
53 // Try each direction from this cell
54 for (const [dx, dy] of directions) {
55 const result = this.checkWordAtPosition(searchWord, row, col, dx, dy);
56 if (result) {
57 return result;
58 }
59 }
60 }
61 }
62
63 // Edge Case: Word not found in grid
64 return undefined;
65 }
66
67 /**
68 * Check if word exists at given position going in given direction
69 * @param {string} word
70 * @param {number} startRow
71 * @param {number} startCol
72 * @param {number} dx
73 * @param {number} dy
74 * @returns {{start: number[], end: number[]}|undefined}
75 */
76 checkWordAtPosition(word, startRow, startCol, dx, dy) {
77 // Edge Case: First letter doesn't match
78 if (this.grid[startRow][startCol] !== word[0]) {
79 return undefined;
80 }
81
82 const endRow = startRow + dx * (word.length - 1);
83 const endCol = startCol + dy * (word.length - 1);
84
85 // Edge Case: Word goes out of bounds
86 if (endRow < 0 || endRow >= this.rows || endCol < 0 || endCol >= this.cols) {
87 return undefined;
88 }
89
90 // Check each letter of the word
91 for (let i = 0; i < word.length; i++) {
92 const row = startRow + dx * i;
93 const col = startCol + dy * i;
94
95 // Edge Case: Letter mismatch at position
96 if (this.grid[row][col] !== word[i]) {
97 return undefined;
98 }
99 }
100
101 // Convert to 1-indexed coordinates as per expected output format
102 return {
103 start: [startCol + 1, startRow + 1],
104 end: [endCol + 1, endRow + 1]
105 };
20106 }
21107}
22108
23109export default WordSearch;
110
111// Handled Edge Cases: Empty word, Empty grid, Word longer than grid dimensions, Word not found in grid, First letter doesn't match, Word goes out of bounds, Letter mismatch at position
Test NameStatus
Can accept a target search word
Fail
Should locate a word written left to right
Fail
Can locate a left to right word in a different position
Fail
Can locate a different left to right word
Fail
Can locate that different left to right word in a different position
Fail
Can locate a left to right word in a two line grid
Fail
Can locate a left to right word in a different position in a two line grid
Fail
Can locate a left to right word in a three line grid
Fail
Can locate a left to right word in a ten line grid
Fail
Can locate a left to right word in a different position in a ten line grid
Fail
Can locate a different left to right word in a ten line grid
Fail
Can find two words written left to right
Fail
Should locate a single word written right to left
Fail
Should locate multiple words written in different horizontal directions
Fail
Should locate words written top to bottom
Fail
Should locate words written bottom to top
Fail
Should locate words written top left to bottom right
Fail
Should locate words written bottom right to top left
Fail
Should locate words written bottom left to top right
Fail
Should locate words written top right to bottom left
Fail
Should fail to locate a word that is not in the puzzle
Fail
Should fail to locate words that are not on horizontal, vertical, or diagonal lines
Fail
Should not concatenate different lines to find a horizontal word
Fail
Should not wrap around horizontally to find a word
Fail
Should not wrap around vertically to find a word
Fail
Should accept an initial game grid
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.