退房我的解决方案为FreeCodeCamp's Advanced Algorithm: No Repeats challenge:FreeCodeCamp挑战:解释错误消息?
Return the number of total permutations of the provided string that don't have repeated consecutive letters.
正确的代码应该返回。有人可以向我解释这些错误信息吗?
RangeError: Maximum call stack size exceeded
at findFactorial:14:24
at findFactorial:21:14
at findFactorial:21:14
at findFactorial:21:14
at findFactorial:21:14
at findFactorial:21:14
注:为了获得在错误的确切行号,复制&这里粘贴代码:https://repl.it/
function permAlone(str) {
var final, factorial, repeated, i;
repeated = str.match(/([a-z])(?:.*)(\1)+/g);
if (str.length < 2) {
return 1;
}
// should return ["aa", "ff"]
if (repeated[0] === str) {
repeated[0] = repeated[0].split('').sort().join('').match(/([a-z])(?:.*)(\1)+/g);
repeated = repeated.reduce(function(a, b) {
return a.concat(b);
});
}
function findFactorial(n) {
if (n < 0) {
alert("No negative numbers accepted.");
}
if (n === 0) {
return 1;
}
return n * findFactorial(n - 1);
}
factorial = findFactorial(str.length); // 7! = 5040
for (i = 0; i < repeated.length; i++) {
i++;
if (repeated.length === 1 && repeated.join("") !== str) {
final = factorial - findFactorial((str.length - 1)) * findFactorial(repeated[0].length);
} else if (repeated.length > 1 && repeated[i-1].length>2 || repeated[i].length>2) {
final = findFactorial(repeated[i].length) * findFactorial(repeated[i - 1].length);
} else {
final = factorial - ((findFactorial((str.length - 1) * repeated[i].length) * (findFactorial(str.length - 1) * repeated[i - 1].length))) + (findFactorial(str.length - 2) * findFactorial(repeated[i - 1]) * findFactorial(repeated[i]));
// final = 5040 - ((6! * 2!)*2) + (5! * 2! * 2!);
}
}
return final;
}
permAlone('abfdefa'); // should return 2640
当您处于无限循环时会发生这种情况。检查你的递归,以确保它应该退出。 –