简体   繁体   English

Return返回Javascript中未定义的返回

[英]Return returns undefined in Javascript

I need to write a program to traverse a maze in Javascript where 1 is the source and 2 is the destination and 3 is a path and 0 is a wall. 我需要编写一个程序来遍历Java迷宫,其中1是源,2是目标,3是路径,0是墙。 Below is the code which I have written. 下面是我编写的代码。 The code is correct but I get the answer only when I use console.log() and not when I return the strings yes or no. 该代码是正确的,但是只有在使用console.log()时我才能得到答案,而在返回字符串yes或no时则无法得到答案。 Why does it happen so? 为什么会这样呢?

function sourceDes(arr) {
for (var i=0;i<arr.length;i++) {
    for (var j=0;j<arr[i].length; j++) {
        if (arr[i][j] == 1) {
            traverse(i,j);
            break;
        }
    }
}

function traverse(rowPos, colPos) {
            if (arr[rowPos][colPos] == 2) {
                console.log("yes");
                // return "Yes";
            }
            else if (arr[rowPos][colPos] == 3 || arr[rowPos][colPos] == 1) {
                arr[rowPos][colPos] = 9;

                if(rowPos < arr.length - 1) {
                    return traverse(rowPos + 1, colPos);
                }
                if(colPos < arr[rowPos].length - 1) {
                    return traverse(rowPos, colPos + 1);
                }
                if(rowPos > 0) {
                    return traverse(rowPos - 1, colPos);
                }
                if(colPos > 0) {
                    return traverse(rowPos, colPos - 1);
                }
            }               
            else if (arr[rowPos][colPos] == 0) {
                console.log("No");
                // return "No";
            }
        }
}

input 输入

arr = [[3, 0, 0, 0], [0, 3, 3, 0], [0, 1, 0, 3], [0, 2, 3, 3]];
answer = sourceDes(arr);
answer;

output when console.log() 当console.log()时输出

yes

output when value is returned and console.log is applied on function 返回值并在功能上应用console.log时输出

undefined

您在sourceDes函数中缺少return语句

return语句应同时在两个函数中,否则返回undefined

In Javascript when you omit a return statement inside a function then an implicit return undefined is assumed. 在Javascript中,当您在函数内省略return语句时,将假定为隐式的return undefined That's why you get this returned value. 这就是为什么您获得此返回值。

I have checked your code and found a bug in your sourceDes function. 我检查了您的代码,并在您的sourceDes函数中发现了一个错误。 You do not define the return statement in the function. 您没有在函数中定义return语句。

function sourceDes(arr) {
for (var i=0;i<arr.length;i++) {
    for (var j=0;j<arr[i].length; j++) {
        if (arr[i][j] == 1) {
            traverse(i,j);
            break;
        }
    }
}

When the loop run for last time, your if condition is getting false that is why it iss returing undefined . 当循环最后一次运行时,您的if条件变为false,这就是为什么要重新定义undefined的原因 Check code below I Have added the else part and it return No when your if condition is false. 检查下面的代码,我添加了else部分,如果条件为false,则返回No。

 arr = [[3, 0, 0, 0], [0, 3, 3, 0], [0, 1, 0, 3], [0, 2, 3, 3]]; function sourceDes(arr) { for (var i=0;i<arr.length;i++) { for (var j=0;j<arr[i].length; j++) { if (arr[i][j] == 1) { traverse(i,j); break; } else{ return "No"; } } } function traverse(rowPos, colPos) { if (arr[rowPos][colPos] == 2) { console.log("yes"); return "Yes"; } else if (arr[rowPos][colPos] == 3 || arr[rowPos][colPos] == 1) { arr[rowPos][colPos] = 9; if(rowPos < arr.length - 1) { return traverse(rowPos + 1, colPos); } if(colPos < arr[rowPos].length - 1) { return traverse(rowPos, colPos + 1); } if(rowPos > 0) { return traverse(rowPos - 1, colPos); } if(colPos > 0) { return traverse(rowPos, colPos - 1); } } else if (arr[rowPos][colPos] == 0) { console.log("No"); return "No"; } } } answer = sourceDes(arr); document.write(answer); 

Use following code snippet. 使用以下代码段。 I have added variable and set the value in it in loop and returned the result variable at the end of function. 我添加了变量并在循环中设置了值,并在函数末尾返回了结果变量。

function sourceDes(arr) {
var result='';
for (var i=0;i<arr.length;i++) {
    for (var j=0;j<arr[i].length; j++) {
        if (arr[i][j] == 1) {
           result= traverse(i,j);
            break;
        }
    }
 return result;
}

function traverse(rowPos, colPos) {
            if (arr[rowPos][colPos] == 2) {
                //console.log("yes");
                 return "Yes";
            }
            else if (arr[rowPos][colPos] == 3 || arr[rowPos][colPos] == 1) {
                arr[rowPos][colPos] = 9;

                if(rowPos < arr.length - 1) {
                    return traverse(rowPos + 1, colPos);
                }
                if(colPos < arr[rowPos].length - 1) {
                    return traverse(rowPos, colPos + 1);
                }
                if(rowPos > 0) {
                    return traverse(rowPos - 1, colPos);
                }
                if(colPos > 0) {
                    return traverse(rowPos, colPos - 1);
                }
            }               
            else if (arr[rowPos][colPos] == 0) {
                //console.log("No");
                 return "No";
            }
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM