I have following recursive function which should call itself unless coordinates goes beyond DOM table or unless distance between starting point and point of current recursion is greater than distance given by user. However function falls into infinite loop switching infinitely between several points and I can't figure out what I have done 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<0 || y<0 || x>boardWidth-1 || y>boardHeight-1){
return;
}else if(getDistance(cells[getFieldId(xStart,yStart)], cells[getFieldId(x,y)]) > 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 help will be greatly appreciated.
fill(x,y+1);will be called again by the recursivefill(x,y-1);call and vice versa. There are tons of multiple calls. This function is just wrong in so many ways