gpt4 book ai didi

javascript - 使用 .length 时获取 "typeError" "is not a function"

转载 作者:搜寻专家 更新时间:2023-11-01 05:28:17 27 4
gpt4 key购买 nike

这是我的代码:

function permAlone(string) {
if (string.length < 2) return string; // This is our break condition

var permutations = []; // This array will hold our permutations

for (var i = 0; i < string.length; i++) {
var character = string[i];

// Cause we don't want any duplicates:
if (string.indexOf(character) != i) // if char was used already
continue; // skip it this time

var remainingString = string.slice(0, i) + string.slice(i + 1, string.length); //Note: you can concat Strings via '+' in JS

for (var subPermutation of permAlone(remainingString))
permutations.push(character + subPermutation);

}

var permutationsFinal = [];
for (var j = 0; j < (permutations.length); j++) {
if (!(permutations[j].match(/([a-zA-Z])\1/))) {
permutationsFinal.push(permutations[j]);
}
}

return (permutationsFinal.length);
// ^^^^^^^^ if I add this, the error is thrown
}

permAlone('abc');

如果我替换:

return (permutationsFinal);

通过:

return (permutationsFinal.length);

我在控制台中收到此错误:

TypeError: permAlone is not a function

为什么?

感谢您的帮助! :)

最佳答案

这是一个递归函数,如果你返回的不是函数本身所期望的任何东西,那么你将打破递归循环。

关于javascript - 使用 .length 时获取 "typeError" "is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44793094/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com