【发布时间】:2019-09-01 10:40:25
【问题描述】:
您好,我想了解 JavaScript 中的递归。
到目前为止我有:
function countVowels(string) {
let vowelCount = 0;
// if we're not at the end of the string,
// and if the character in the string is a vowel
if (string.length - 1 >= 0 && charAt(string.length -1) === "aeiouAEIOU") {
//increase vowel count every time we iterate
countVowels(vowelCount++);
}
return vowelCount;
}
首先,这给我带来了问题,因为 charAt 没有定义。迭代时我还能怎么说“当前索引处的字符”?
我不能使用 for 循环 - 我必须使用递归。
其次,我在这里正确使用递归吗?
countVowels(vowelCount++);
每次调用该函数时,我都会尝试增加元音计数。
感谢您的指导。
【问题讨论】:
标签: javascript string recursion count