Alone

Finished
11//
2// This is only a SKELETON file for the 'Flower Field' exercise. It's been provided as a
3// convenience to get you started writing code faster.
2// This is only a SKELETON file for the 'Flower Field' exercise. It's been provided as a// convenience to get you started writing code faster.
43//
54
65/**
7 * @param {string[]} input
8 * @returns {string[]}
6 * Annotates a flower field by replacing empty spaces with the count of adjacent flowers.
7 * @param {string[]} input - Array of strings representing the flower field where '*' is a flower and ' ' is empty
8 * @returns {string[]} - Array of strings with empty spaces replaced by flower counts or left empty if no adjacent flowers
99 */
1010export const annotate = (input) => {
11 throw new Error('Remove this statement and implement this function');
12};
11 // Edge Case: Handle empty input array
12 if (!input || input.length === 0) {
13 return [];
14 }
15
16 // Edge Case: Handle input with empty strings
17 const rows = input.length;
18 const cols = input[0] ? input[0].length : 0;
19
20 // Edge Case: Handle rows with inconsistent lengths
21 for (let i = 0; i < rows; i++) {
22 if (input[i].length !== cols) {
23 throw new Error('All rows must have the same length');
24 }
25 }
26
27 // Create a result array with the same dimensions as input
28 const result = Array(rows);
29
30 // Define the 8 possible directions (horizontal, vertical, diagonal)
31 const directions = [
32 [-1, -1], [-1, 0], [-1, 1],
33 [0, -1], [0, 1],
34 [1, -1], [1, 0], [1, 1]
35 ];
36
37 // Process each cell in the grid
38 for (let i = 0; i < rows; i++) {
39 let newRow = '';
40 for (let j = 0; j < cols; j++) {
41 // If the cell is a flower, keep it as is
42 if (input[i][j] === '*') {
43 newRow += '*';
44 } else {
45 // Count adjacent flowers
46 let count = 0;
47
48 // Check all 8 directions
49 for (const [dx, dy] of directions) {
50 const ni = i + dx;
51 const nj = j + dy;
52
53 // Check if the adjacent cell is within bounds
54 if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
55 // If the adjacent cell is a flower, increment count
56 if (input[ni][nj] === '*') {
57 count++;
58 }
59 }
60 }
61
62 // If no adjacent flowers, leave empty, otherwise put the count
63 if (count === 0) {
64 newRow += ' ';
65 } else {
66 newRow += count.toString();
67 }
68 }
69 }
70 result[i] = newRow;
71 }
72
73 return result;
74 // Handled Edge Cases: Empty input array, input with empty strings, rows with inconsistent lengths
75};
Test NameStatus
handles no rows
Pass
handles no columns
Pass
handles no flowers
Pass
handles garden full of flowers
Pass
handles flower surrounded by spaces
Pass
handles space surrounded by flowers
Pass
handles horizontal line
Pass
handles horizontal line, flowers at edges
Pass
handles vertical line
Pass
handles vertical line, flowers at edges
Pass
handles cross
Pass
handles large garden
Pass

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