英文:
JavaScript function to check for vowels
问题
这是我目前拥有的代码:
const myString = "Testing for vowels";
const vowels = ["a", "e", "i", "o", "u", "y"];
function countVowels(myString) {
let count = 0;
if (vowels.includes(myString)) {
count++;
}
return count
}
const result = countVowels(myString);
console.log(result);
这段代码的问题在于你尝试检查整个字符串是否包含在 vowels
数组中,而不是检查字符串中的每个字符是否在 vowels
中出现。要修复这个问题,你需要使用一个循环来遍历字符串中的每个字符,然后检查该字符是否在 vowels
数组中。
以下是修复后的代码:
const myString = "Testing for vowels";
const vowels = ["a", "e", "i", "o", "u", "y"];
function countVowels(myString) {
let count = 0;
for (let i = 0; i < myString.length; i++) {
if (vowels.includes(myString[i])) {
count++;
}
}
return count;
}
const result = countVowels(myString);
console.log(result);
这个修复后的代码将遍历字符串 myString
中的每个字符,然后检查它是否是元音字母,并适当地增加计数器。
英文:
I know this is a basic questions, but I'm trying to work through a beginner's coding problem and I'm stuck on why this code doesn't work.
This is what I have so far:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const myString = "Testing for vowels";
const vowels = ["a", "e", "i", "o", "u", "y"];
function countVowels(myString) {
let count = 0;
if (vowels.includes(myString)) {
count++;
}
return count
}
const result = countVowels(myString);
console.log(result);
<!-- end snippet -->
When it logs out, it just stays 0
. Clearly I'm missing something, but I'm not sure what it is at this point. I've set up what vowels are, I think I'm asking that, if the vowels I set in the array are in my string, increase the count, but it's just giving me 0
. I know it must be obvious, but I'm stuck.
I've tried a for
loop, then I thought it may need to be an if
statement, now I have that in a function. I haven't been able to get the answer either way, so I don't think I'm properly telling the code what the vowels are, but I'm not sure how to do that.
答案1
得分: 2
你需要使用一个for循环来检查每个字符是否包含在元音字母数组中,代码如下:
const myString = "Testing for vowels";
const vowels = ["a", "e", "i", "o", "u", "y"];
function countVowels(myString) {
let count = 0;
for(let i = 0; i < myString.length; i++) {
if(vowels.includes(myString[i])) {
count++;
}
}
return count;
}
const result = countVowels(myString);
console.log(result);
这段代码会计算在myString
中有多少个元音字母,并将结果输出到控制台。
英文:
you have to use a for loop in order to check if each character is included in the vowels array like this :
const myString = "Testing for vowels";
const vowels = ["a", "e", "i", "o", "u", "y"];
function countVowels(myString) {
let count = 0;
for(let i = 0 ; i < myString.length ; i++) {
if(vowels.includes(myString[i])) {
count++ ;
}
}
return count
}
const result = countVowels(myString);
console.log(result);
答案2
得分: 1
以下是您要翻译的内容:
在不区分大小写并全局标记的正则表达式中,像 /[aeiou]/gi
这样的表达式在字符集中列出了所有元音字母,并通过 match
应用,已经完成了任务。后一操作要么返回一个数组,要么返回 null
值。因此,像下面这样的表达式...
'Testing for vowels'.match(/[aeiou]/gi)?.length ?? 0
...就是所需的全部内容。
为了防止在匹配为 null
的情况下出现 TypeError
,其中无法访问 length
属性,但仍希望得到有意义的结果,可以同时使用Optional Chaining Operator / ?.
和Nullish Coalescing Operator / ??
。
示例代码...
console.log(
"'Testing for vowels'.match(/[aeiou]/gi) ...",
'Testing for vowels'.match(/[aeiou]/gi)
);
console.log(
"'' .match(/[aeiou]/gi) ...",
''.match(/[aeiou]/gi)
);
console.log('\n');
console.log(
"'Testing for vowels'.match(/[aeiou]/gi)?.length ?? 0 ...",
'Testing for vowels'.match(/[aeiou]/gi)?.length ?? 0
);
console.log(
"'' .match(/[aeiou]/gi)?.length ?? 0 ...",
''.match(/[aeiou]/gi)?.length ?? 0
);
然后,countVowels
的实现就像这样简单...
function countVowels(value) {
return String(value).match(/[aeiou]/gi)?.length ?? 0;
}
...或者像这样...
function countVowels(value) {
return String(value ?? '').match(/[aeiou]/gi)?.length ?? 0;
}
...其中后者可以如下进行测试...
function countVowels(value) {
return String(value ?? '').match(/[aeiou]/gi)?.length ?? 0;
}
console.log(
"countVowels('Testing for vowels') ...",
countVowels('Testing for vowels')
);
console.log(
"countVowels('') ...",
countVowels('')
);
console.log('\n');
console.log(
"countVowels() ...",
countVowels()
);
console.log(
"countVowels(null) ...",
countVowels(null)
);
console.log('\n');
console.log(
"countVowels('NULL') ...",
countVowels('NULL')
);
console.log(
"countVowels('UNDEFINED') ...",
countVowels('UNDEFINED')
);
英文:
A simple case-insensitive and globally flagged regex like ... /[aeiou]/gi
... which within a character set does list all vowels, applied via match
, already does the job. The latter operation either returns an array or the null
value. Thus, an expression like ...
'Testing for vowels'.match(/[aeiou]/gi)?.length ?? 0
... is all what's needed.
And in order to prevent a TypeError
in case of a null
match, where one can not access a length
property, but still wants to come up with a meaningful result, one might utilize both the Optional Chaining Operator / ?.
and the Nullish Coalescing Operator / ??
.
Example code ...
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
console.log(
"'Testing for vowels'.match(/[aeiou]/gi) ...",
'Testing for vowels'.match(/[aeiou]/gi)
);
console.log(
"''.match(/[aeiou]/gi) ...",
''.match(/[aeiou]/gi)
);
console.log('\n');
console.log(
"'Testing for vowels'.match(/[aeiou]/gi)?.length ?? 0 ...",
'Testing for vowels'.match(/[aeiou]/gi)?.length ?? 0
);
console.log(
"''.match(/[aeiou]/gi)?.length ?? 0 ...",
''.match(/[aeiou]/gi)?.length ?? 0
);
<!-- language: lang-css -->
.as-console-wrapper { min-height: 100%!important; top: 0; }
<!-- end snippet -->
The implementation of countVowels
then is as easy as this ...
function countVowels(value) {
return String(value).match(/[aeiou]/gi)?.length ?? 0;
}
... or that ...
function countVowels(value) {
return String(value ?? '').match(/[aeiou]/gi)?.length ?? 0;
}
... where the latter could be tested like follows ...
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function countVowels(value) {
return String(value ?? '').match(/[aeiou]/gi)?.length ?? 0;
}
console.log(
"countVowels('Testing for vowels') ...",
countVowels('Testing for vowels')
);
console.log(
"countVowels('') ...",
countVowels('')
);
console.log('\n');
console.log(
"countVowels() ...",
countVowels()
);
console.log(
"countVowels(null) ...",
countVowels(null)
);
console.log('\n');
console.log(
"countVowels('NULL') ...",
countVowels('NULL')
);
console.log(
"countVowels('UNDEFINED') ...",
countVowels('UNDEFINED')
);
<!-- language: lang-css -->
.as-console-wrapper { min-height: 100%!important; top: 0; }
<!-- end snippet -->
答案3
得分: -1
const myString = "测试元音字母";
const vowels = ["a", "e", "i", "o", "u", "y"];
function countVowels(myString) {
let count = 0;
for(i = 0; i < myString.length; i++){
if (vowels.includes(myString.substring(i,i+1))) {
count++;
}
}
return count
}
const result = countVowels(myString);
console.log(result);
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const myString = "Testing for vowels";
const vowels = ["a", "e", "i", "o", "u", "y"];
function countVowels(myString) {
let count = 0;
for(i = 0; i < myString.length; i++){
if (vowels.includes(myString.substring(i,i+1))) {
count++;
}
}
return count
}
const result = countVowels(myString);
console.log(result);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论