Uncaught RangeError: Maximum call stack size exceeded, recursion with random numbers?
Uncaught RangeError: Maximum call stack size exceeded, recursion with random numbers?
所以我知道最大调用堆栈错误是在发生无限循环时通过递归出现的。但是我遇到了这个奇怪的问题。下面代码中的所有内容都可以正常工作,直到我在 randomRow 和 randomCol 下添加这 2 条注释行。是否可以使用随机数创建无限循环,或者我是否遗漏了什么?
function insertNumbers(freeRCPos, grid, fields, num) {
//base case
if (fields.length === 0) return grid;
let randomRow = Math.floor(Math.random() * maxRows);
let randomCol = Math.floor(Math.random() * maxRows);
// if (grid[randomRow][randomCol].value !== undefined)
// return insertNumbers(freeRCPos, grid, fields, num);
let newFields = [];
//there are in total 9 fields so it has to go through all of them
for (let field of fields) {
//check if its already in field
if (field.includes(JSON.stringify([randomRow, randomCol]))) {
let res = freeRCPos.map((element: any) =>
element.map((pos: any) =>
pos === JSON.stringify([randomRow, randomCol]) ? true : false
)
);
if (
res.filter((el: any) => (el.includes(true) ? true : false)).length > 0
) {
grid[randomRow][randomCol].value = num;
newFields= fields.filter(
(_: any, fieldId: number) => fields.indexOf(field) !== fieldId
);
//return with modified fields and others to be able to complete base case
return insertNumbers(
freeRCPos.map((field: any) =>
field.map((pos: string) =>
pos?.startsWith("[" + randomRow) || pos?.endsWith(randomCol + "]")
? null
: pos
)
),
grid,
newFields,
num,
);
//else return same
} else {
return insertNumbers(freeRCPos, grid, fields, num);
}
//else return same
} else {
return insertNumbers(freeRCPos, grid, fields, num);
}
}
return grid;
}
我可能在这里放了很多不必要的代码,所以如果我需要缩短它,尽管问。感谢帮助。
如果随机数是grid
中的现有索引(=如果grid[randomRow][randomCol].value !== undefined
为真),它永远不会通过注释部分,它会继续用相同的值调用自己。您需要在递归调用函数之前更新一些内容并更接近 fields.length === 0
.
的基本情况
所以我知道最大调用堆栈错误是在发生无限循环时通过递归出现的。但是我遇到了这个奇怪的问题。下面代码中的所有内容都可以正常工作,直到我在 randomRow 和 randomCol 下添加这 2 条注释行。是否可以使用随机数创建无限循环,或者我是否遗漏了什么?
function insertNumbers(freeRCPos, grid, fields, num) {
//base case
if (fields.length === 0) return grid;
let randomRow = Math.floor(Math.random() * maxRows);
let randomCol = Math.floor(Math.random() * maxRows);
// if (grid[randomRow][randomCol].value !== undefined)
// return insertNumbers(freeRCPos, grid, fields, num);
let newFields = [];
//there are in total 9 fields so it has to go through all of them
for (let field of fields) {
//check if its already in field
if (field.includes(JSON.stringify([randomRow, randomCol]))) {
let res = freeRCPos.map((element: any) =>
element.map((pos: any) =>
pos === JSON.stringify([randomRow, randomCol]) ? true : false
)
);
if (
res.filter((el: any) => (el.includes(true) ? true : false)).length > 0
) {
grid[randomRow][randomCol].value = num;
newFields= fields.filter(
(_: any, fieldId: number) => fields.indexOf(field) !== fieldId
);
//return with modified fields and others to be able to complete base case
return insertNumbers(
freeRCPos.map((field: any) =>
field.map((pos: string) =>
pos?.startsWith("[" + randomRow) || pos?.endsWith(randomCol + "]")
? null
: pos
)
),
grid,
newFields,
num,
);
//else return same
} else {
return insertNumbers(freeRCPos, grid, fields, num);
}
//else return same
} else {
return insertNumbers(freeRCPos, grid, fields, num);
}
}
return grid;
}
我可能在这里放了很多不必要的代码,所以如果我需要缩短它,尽管问。感谢帮助。
如果随机数是grid
中的现有索引(=如果grid[randomRow][randomCol].value !== undefined
为真),它永远不会通过注释部分,它会继续用相同的值调用自己。您需要在递归调用函数之前更新一些内容并更接近 fields.length === 0
.