为什么我不能从递归函数返回?

时间:2019-04-24 03:26:03

标签: javascript recursion return

好的Stackoverflow首先在这里发布。我很茫然,不胜感激。

我有一个递归函数,在我尝试从中返回之前,它似乎一直可以正常工作。 (只是一个迷宫赛跑者很有趣)

到达return语句时,似乎循环回到if(grid[y+1][x] ===2)语句。
然后,我看不到该函数再次被调用,但是yx的值开始变化,因为它在遍历函数之前在到达底部的else并返回那里

我还在控制台中看到“内部return if”,并且在浏览器中设置断点显示return语句被调用,只是不退出该函数。

非常感谢!

const grid =[0,0,0,0,3,2], 
            [0,0,0,0,3,0],
            [0,0,0,0,3,0],
            [3,3,3,3,3,0],
            [0,0,0,0,0,0],
            [1,3,3,3,3,3]
        ],

const recursiveSearch = (y, x) => { // init values y=5 x=0


    console.log(y + '  ' + x);
    if(grid[y-1]){
        if(grid[y-1][x] === 0) {
            grid[y-1][x] = 4;
            recursiveSearch(y-1, x);
        }
        if(grid[y-1][x] === 2) {
            console.log('inside return if');
            return 'end point found at';
        }
    }
    if(grid[y][x+1] || grid[y][x+1] === 0) {
        if(grid[y][x+1] === 0) {
            grid[y][x+1] = 4;
            recursiveSearch(y,x+1);
        }
        if(grid[y][x+1] === 2) {
            return "end point found at";
        }
    }
    if(grid[y+1] || grid[y+1] === 0) {
        if(grid[y+1][x] === 0) {
            grid[y+1][x] = 4;
            recursiveSearch(y+1,x);
        }
        if(grid[y+1][x] === 2) {
            return "end point found at";
        }
    }
    if(grid[y][x-1] || grid[y][x-1] === 0 ) {
        if(grid[y][x-1] === 0) {
            grid[y][x-1] = 4;
            recursiveSearch(y,x-1);
        };
        if(grid[y][x-1] === 2) {
            return "end point found at";
        }
    } else {
        return "No solution found"
    }
};

2 个答案:

答案 0 :(得分:1)

您必须返回调用递归函数的值。

const grid =[0,0,0,0,3,2], 
            [0,0,0,0,3,0],
            [0,0,0,0,3,0],
            [3,3,3,3,3,0],
            [0,0,0,0,0,0],
            [1,3,3,3,3,3]
        ],

const recursiveSearch = (y, x) => { // init values y=5 x=0


    console.log(y + '  ' + x);
    if(grid[y-1]){
        if(grid[y-1][x] === 0) {
            grid[y-1][x] = 4;
            return recursiveSearch(y-1, x); // must return value of calling recursiveSearch
        }
        if(grid[y-1][x] === 2) {
            console.log('inside return if');
            return 'end point found at';
        }
    }
    if(grid[y][x+1] || grid[y][x+1] === 0) {
        if(grid[y][x+1] === 0) {
            grid[y][x+1] = 4;
            return recursiveSearch(y,x+1); // // must return value of calling recursiveSearch
        }
        if(grid[y][x+1] === 2) {
            return "end point found at";
        }
    }
    if(grid[y+1] || grid[y+1] === 0) {
        if(grid[y+1][x] === 0) {
            grid[y+1][x] = 4;
            return recursiveSearch(y+1,x); // must return value of calling recursiveSearch
        }
        if(grid[y+1][x] === 2) {
            return "end point found at";
        }
    }
    if(grid[y][x-1] || grid[y][x-1] === 0 ) {
        if(grid[y][x-1] === 0) {
            grid[y][x-1] = 4;
            return recursiveSearch(y,x-1); // must return value of calling recursiveSearch
        };
        if(grid[y][x-1] === 2) {
            return "end point found at";
        }
    } else {
        return "No solution found"
    }
};

答案 1 :(得分:0)

请为每个递归调用添加return语句。