I have a recursive function that is supposed to call itself until the coordinates exceed the boundaries of a DOM table or until the distance between the starting point and the current recursion point is greater than the user-defined distance. However, the function seems to be stuck in an infinite loop, constantly switching between several points and I can't seem to pinpoint what went wrong.
function fillSquare(a,b,dist){
var xStart = parseInt(a);
var yStart = parseInt(b);
var distance = dist;
function fill(c,d){
var x = parseInt(c);
var y = parseInt(d);
if(x<span class="operator"><</span>0 || y<span class="operator"><</span>0 || x<span class="operator">></span>boardWidth-1 || y<span class="operator">></span>boardHeight-1){
return;
}else if(getDistance(cells[getFieldId(xStart,yStart)], cells[getFieldId(x,y)]) <span class="operator">></span> dist){
return;
}else{
cells[getFieldId(x,y)].hasWall = false;
document.getElementById(x+'x'+y).backgroundColor = 'gray';
console.log(x+' '+y);
fill(x-1,y);
fill(x+1,y);
fill(x,y-1);
fill(x,y+1);
}
}
fill(xStart,yStart);
}
Any assistance on this matter would be highly appreciated.