如何在Javascript中返回字符串中元音的数量?
如果有人可以查看我的代码,我会很高兴。我一直在尝试返回给定字符串中的元音总数。我希望我的代码能够考虑字母的大小写、字符串中的任何空格以及元音的缺失。我相信我的 if-statement 有问题,对于字符串输入“Crypt”和“Crypto”返回 0
function countVowels(str) {
let count = 0;
let arr = str.toLowerCase().split("")
let vowels = ["a","e","i","o","u"]
console.log(arr)
for (let i = 0; i < str.length; i++){
if (arr[i].includes(vowels)){
count++
} else {
return 0
}
}
return count
}
console.log(countVowels("Crypto"))
console.log(countVowels("Crypt"))
I would be delighted if someone could look into my code. I have been trying to return the total number of vowels in a given string. I want my code to take into account of cases of the letter, any empty spaces in the string and also absence of vowel. I believe there is something wrong with my if-statement which is returning 0 for the string input "Crypt" and "Crypto"
function countVowels(str) {
let count = 0;
let arr = str.toLowerCase().split("")
let vowels = ["a","e","i","o","u"]
console.log(arr)
for (let i = 0; i < str.length; i++){
if (arr[i].includes(vowels)){
count++
} else {
return 0
}
}
return count
}
console.log(countVowels("Crypto"))
console.log(countVowels("Crypt"))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您在此处生成的代码存在几个问题。
第一个是,对于字符串中的每个字母,您要检查该字母是否包含元音数组,这永远不会是真的。你已经有效地写了这个......
这是倒退的。
相反,您需要询问数组是否包含给定的字母,如下所示:
第二个问题是,在您的
else
分支中,您返回 0
。当您第一次遇到非元音时,这将立即停止您的函数并返回0
。您可以不采取任何操作,完全删除else
分支,而不是return 0
。最终你的循环应该是这样的:
There are several problems with the code you've produced here.
The first is that, for each letter in the string, you're checking whether the letter includes the array of vowels, which will never be true. You've effectively written this...
which is backwards.
Instead you need to ask the array whether it includes the given, letter, this way:
The second problem is that, in your
else
branch, youreturn 0
. This will immediately halt your function and return0
the first time you encounter a non-vowel. Instead ofreturn 0
, you can simply take no action, and drop theelse
branch entirely.Ultimately your loop should look like this:
有很多解决方案。
我认为你才刚刚开始所以我希望以下内容会有所帮助
there are a lo of solution to this.
i think you are just starting so i hope following will help
在
for
循环中,表达式:.includes()
是一个字符串和一个数组方法,因此新手可能会感到困惑。一般来说,您希望在方法前面加上用作过滤器的数组或字符串(用于与输入值进行比较的字符串/数组)。此外,
else
语句将使您的函数短路,因为return
立即结束整个函数。if
语句就足够了,如果没有匹配项,那么for
循环将忽略它。作为更有效的替代方案,请像这样使用
.filter()
并返回V.length
:In your
for
loop, the expression:.includes()
is a String and an Array method so it can be confusing to a novice. In general, you want to prefix the method with array or string that serves as the filter (the string/array used to compare with the input value).Also, the
else
statement will short-circut your function becausereturn
ends the whole function immediately. Theif
statement is sufficient, if there isn't a match then thefor
loop will just ignore it.As a more efficient alternative use
.filter()
like so and returnV.length
:语言很复杂。英语中的元音字符比数组
['a', 'e', 'i', 'o', 'u']
中包含的元音字符多,主要是因为出现在借用词,例如“咖啡馆”。因此,在尝试检测字符串中的元音之前,您可能需要使用 String.prototype.normalize 与
'NFD'
标志将重音字符分解为基本字符和控制字符。对于非重音字符串,例如
'Testing'
,结果应与输入相同。但对于重音字符串,例如'Café'
,结果会将任何重音字符分解为其基本字符和控制字符:完成此分解后,您就可以使用未修改的元音列表。我建议使用 String.prototype.split 将字符串转换为数组,然后 Array.prototype.filter 从数组中删除所有非元音字符,以便您可以读取其长度:
Language is complicated. There are more vowel characters in English than are contained in the array
['a', 'e', 'i', 'o', 'u']
, mainly because of accents that show up in borrowed words such as "Café".So before you try to detect vowels in a string, you may want to decompose it using String.prototype.normalize with the
'NFD'
flag to decompose accented characters into their base and a control character.For an unaccented string, such as
'Testing'
, the result should be identical to the input. But for an accented string, such as'Café'
, the result will decompose any accented characters into their base character and a control character:Once you've done this decomposition, then you can use your list of unmodified vowels. I'd recommend using String.prototype.split to convert the string into an array, and then Array.prototype.filter to remove all non-vowel characters from the array so you can read its length:
它应该是 Array.includes(string) 而不是 String.includes(Array)。
工作演示:
It should be
Array.includes(string)
instead ofString.includes(Array)
.Working Demo :