Tell us what’s happening:
My solution to the challenge passes all but 1 test. Test #5 claims the grid can be filled in 5 click but my solution can do it in 4. Am I wrong or is the test wrong?
Your code so far
function bucketFill(grid, targetColor) {
const gridClone = structuredClone(grid);
function changeRegion(x, y, color, intermediate = "", dir = null) {
if (x < 0 || x >= gridClone.length || y < 0 || y >= gridClone[x].length) return;
if (gridClone[x][y] === color) {
gridClone[x][y] = targetColor;
changeRegion(x - 1, y, color); // Up
changeRegion(x + 1, y, color); // Down
changeRegion(x, y - 1, color); // Left
changeRegion(x, y + 1, color); // Right
} else if (!intermediate) {
intermediate = gridClone[x][y];
changeRegion(x - 1, y, color, intermediate, [-1, 0]); // Up
changeRegion(x + 1, y, color, intermediate, [1, 0]); // Down
changeRegion(x, y - 1, color, intermediate, [0, -1]); // Left
changeRegion(x, y + 1, color, intermediate, [0, 1]); // Right
} else if (gridClone[x][y] === intermediate) {
changeRegion(x + dir[0], y + dir[1], color, intermediate, dir); // Straight Line
}
}
let clicks = 0;
for (let i = 0; i < gridClone.length; i++) {
for (let j = 0; j < gridClone[i].length; j++) {
const cellColor = gridClone[i][j];
if (cellColor === targetColor) continue;
clicks++;
changeRegion(i, j, cellColor);
}
}
return clicks;
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36
Challenge Information:
Daily Coding Challenge - The Last Challenge: Bucket Fill 3
https://www.freecodecamp.org/learn/daily-coding-challenge/08-10
GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/daily-coding-challenges-javascript/6a26df95efa55a2524399746.md at main · freeCodeCamp/freeCodeCamp · GitHub