我写了一个关于在单词中查找元音的代码。感谢console.log,我得到了输出,它可以工作。但是一旦我尝试返回,它不会返回任何东西..我不明白为什么?这里的代码:function vowelCount(str) { let word = str.split(""); // console.log(word); // console.log(word.length - 1); let count = 0; for (let i = 0; i <= word.length - 1; i++) { if ( str[i] === "a" || str[i] === "e" || str[i] === "i" || str[i] === "o" || str[i] === "u" || str[i] === "y" ) { count = count + 1; } } // console.log(count); return count;}vowelCount("hello");vowelCount("thereactor");
4 回答

慕斯709654
TA贡献1840条经验 获得超5个赞
确保使用返回的数据。
var vowelsInHello = vowelCount("hello");
console.log(vowelsInHello);

白衣非少年
TA贡献1155条经验 获得超0个赞
这是一个更好的方法
function vowelCount(str) {
let count = 0;
for (let i = 0; i <= str.length - 1; i++) {
var char = str.charAt(i).toLowerCase()
if (
char === "a" ||
char === "e" ||
char === "i" ||
char === "o" ||
char === "u" ||
char === "y"
) {
count++
}
}
return count;
}
console.log(vowelCount('this has some vowels'))
添加回答
举报
0/150
提交
取消