Finished
44//
55
66export const annotate = (input) => {
7 throw new Error('Remove this line and implement the function');
7 // Edge Case: Empty board
8 if (input.length === 0) {
9 return [];
10 }
11
12 const rows = input.length;
13 const cols = input[0].length;
14
15 // Edge Case: Board with no columns
16 if (cols === 0) {
17 return input;
18 }
19
20 // Create a result board with the same dimensions
21 const result = Array(rows).fill().map(() => Array(cols).fill(' '));
22
23 // Define the 8 possible directions (horizontal, vertical, diagonal)
24 const directions = [
25 [-1, -1], [-1, 0], [-1, 1],
26 [0, -1], [0, 1],
27 [1, -1], [1, 0], [1, 1]
28 ];
29
30 // Process each cell
31 for (let i = 0; i < rows; i++) {
32 for (let j = 0; j < cols; j++) {
33 // If the cell is a mine, keep it as is
34 if (input[i][j] === '*') {
35 result[i][j] = '*';
36 } else {
37 // Count adjacent mines
38 let mineCount = 0;
39
40 // Check all 8 directions
41 for (const [dx, dy] of directions) {
42 const ni = i + dx;
43 const nj = j + dy;
44
45 // Edge Case: Check boundaries to avoid index out of bounds
46 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
47 if (input[ni][nj] === '*') {
48 mineCount++;
49 }
50 }
51 }
52
53 // Edge Case: If no adjacent mines, leave cell empty
54 // Otherwise, set the count
55 if (mineCount > 0) {
56 result[i][j] = mineCount.toString();
57 }
58 }
59 }
60 }
61
62 // Convert each row array to string
63 return result.map(row => row.join(''));
64
65 // Handled Edge Cases: empty board, board with no columns, cells on edges/corners with fewer neighbors, cells with no adjacent mines
866};
Test NameStatus
handles no rows
Pass
handles no columns
Pass
handles no mines
Pass
handles minefield with only mines
Pass
handles mine surrounded by spaces
Pass
handles space surrounded by mines
Pass
handles horizontal line
Pass
handles horizontal line, mines at edges
Pass
handles vertical line
Pass
handles vertical line, mines at edges
Pass
handles cross
Pass
handles large minefield
Pass

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