退房我的解決方案爲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
當您處於無限循環時會發生這種情況。檢查你的遞歸,以確保它應該退出。 –