[英]Why does this recursive javascript function expression return undefined?
I wrote a solution to this standard everyday homework problem to solve Pascal's Triangle recursively. 我为这个标准的日常作业问题写了一个解决方案,以递归方式解决Pascal的Triangle。 Console.log shows the answer my function derived.
Console.log显示我的函数得出的答案。 Printing to innerHTML of a div writes the answer to the screen.
打印到div的innerHTML会将答案写入屏幕。 Stepping through the javascript debugger in chrome shows the solution array exists right before return returnArray.
逐步浏览chrome中的javascript调试器会显示解决方案数组在返回returnArray之前就存在。 But still at the end of the day, the function returns "undefined".
但是仍然在一天结束时,该函数返回“ undefined”。 I don't get it.
我不明白
var generate = function (numRows) {
var startRow = 1;
var startCol = 1;
var endRow = parseInt(numRows);
var rowArray = [];
var returnArray = [];
var triVal;
var triangle = function (row, col) {
//base case
if (col == 1 || col == row) { //If the element is the either first or last element then initialize it with 1
if (col == 1) {
rowArray = [];
}
triVal = 1;
rowArray.push(triVal);
if (col == row) {
returnArray.push(rowArray);
}
}
else {
//calc
triVal = returnArray[row - 2][col - 2] + returnArray[row - 2][col - 1]
triVal = typeof (triVal) === "undefined" ? 1 : triVal;
rowArray.push(triVal);
}
if (col == row && row == endRow) { //stop the function and return
console.log(JSON.stringify(returnArray));
myDiv2.innerHTML = JSON.stringify(returnArray);
return returnArray;
}
else {
if (col == row) {
col = 1;
row = row + 1;
} else {
col = col + 1;
}
triangle(row, col);
}
}
triangle(startRow, startCol);
};
var test = generate(4);
console.log('test is: ' + test); //Why does it returned undefined
My first solution was in the form function generate(numRows){}; 我的第一个解决方案是使用表单函数generate(numRows){}; but the homework calls for the solution to be in the expression form var generate = function(numRows) {};
但作业要求解决方案采用表达式形式var generate = function(numRows){}; The first way returns my expected result.
第一种方法返回我的预期结果。 The second does not.
第二个没有。 What fundamental of javascript don't I understand?
我不了解javascript的哪些基础知识?
var generate = function (numRows) {
//numRows == 0 ? numRows = 1 : numRows = numRows;
if (numRows == 0) { return [];}
var startRow = 1;
var startCol = 1;
var endRow = parseInt(numRows);
var rowArray = [];
var returnArray = [];
var triVal;
var triangle = function (row, col) {
//base case
if (col == 1 || col == row) { //If the element is the either first or last element then initialize it with 1
if (col == 1) {
rowArray = [];
}
triVal = 1;
rowArray.push(triVal);
if (col == row) {
returnArray.push(rowArray);
}
}
else {
//calc
triVal = returnArray[row - 2][col - 2] + returnArray[row - 2][col - 1]
triVal = typeof (triVal) === "undefined" ? 1 : triVal;
rowArray.push(triVal);
}
if (col == row && row == endRow) { //stop the function and return
console.log(JSON.stringify(returnArray));
myDiv2.innerHTML = JSON.stringify(returnArray);
return returnArray;
}
else {
if (col == row) {
col = 1;
row = row + 1;
} else {
col = col + 1;
}
triangle(row, col);
}
}
triangle(startRow, startCol);
return returnArray; //duh...the obvious answer!!!
};
var test = generate(1);
console.log('test is: ' + (JSON.stringify(test)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.