admin管理员组

文章数量:1426959

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. 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. 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()

yes

output when value is returned and console.log is applied on function

undefined

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. 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. 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()

yes

output when value is returned and console.log is applied on function

undefined
Share Improve this question asked Dec 20, 2018 at 9:45 Lax_SamLax_Sam 1,1392 gold badges20 silver badges38 bronze badges 2
  • Which function do you wrap the console.log around? – Mathyn Commented Dec 20, 2018 at 9:49
  • Did you try using return? Unmenting the lines I mented in the code and then menting console.log lines? – Lax_Sam Commented Dec 20, 2018 at 10:15
Add a ment  | 

5 Answers 5

Reset to default 2

The return statements should be in both functions otherwise undefined is returned

You are missing a return statement in the sourceDes function

In Javascript when you omit a return statement inside a function then an implicit return undefined is assumed. That's why you get this returned value.

I have checked your code and found a bug in your sourceDes function. You do not define the return statement in the function.

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. Check code below I Have added the else part and it return No when your if condition is false.

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";
            }
        }
}

本文标签: Return returns undefined in JavascriptStack Overflow