I'm trying to update biggestIslandLength in my function, but it isn't working. I know a solution would be to use a global variable, but I want to deepen my understanding of JS and want to know how I can get it to work this way. I've attached my code.
// Online Javascript Editor for free
// Write, Edit and Run your Javascript code using JS Online Compiler
function max_area_islands_DFS(matrix) {
let biggestIslandLength = 0;
const MAX_LENGTH = matrix.length;
const MAX_WIDTH = matrix[0].length;
for(let x = 0; x < MAX_LENGTH; x++) {
for(let y = 0; y < MAX_WIDTH; y++) {
DFSIslandSearch(matrix, x, y, 0, biggestIslandLength)
}
}
return biggestIslandLength;
}
function DFSIslandSearch(matrix, x, y, currentCount, biggestIslandLength) {
if(x >4 || x < 0 || y > 4 || y < 0) {
return
}
if(matrix[x][y] === 0) {
return
}
matrix[x][y] = 0;
currentCount += 1;
biggestIslandLength = Math.max(currentCount, biggestIslandLength);
DFSIslandSearch(matrix, x + 1, y, currentCount, biggestIslandLength);
DFSIslandSearch(matrix, x - 1, y, currentCount, biggestIslandLength);
DFSIslandSearch(matrix, x, y + 1, currentCount, biggestIslandLength);
DFSIslandSearch(matrix, x, y - 1, currentCount, biggestIslandLength);
}
console.log(max_area_islands_DFS([
[1, 1, 1, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 1, 1, 0],
[0, 1, 1, 0, 0],
[0, 0, 1, 0, 0]
]));
This code returns 0 every time, but it's supposed to return 3. Thanks!