如何计算具有两个功能的Javascript字符串中的元音? [英] How to count vowels in a Javascript string with two functions?

查看:89
本文介绍了如何计算具有两个功能的Javascript字符串中的元音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过调用该函数中的另一个函数来编写一个计算字符串中元音的Javascript函数,但是当我在控制台中对其进行测试时,它返回0。



这是我的第一个函数,可以正常工作并识别字符串是否是元音:

 函数isVowel(ch) {
var pattern = / [aeiouAEIOU] /
return pattern.test(ch);
};

对于第二个功能,我的想法都没有奏效。以下是到目前为止我尝试过的一些示例:



这个返回我0:

  function countVowels(str){
var count = 0;

for(var i; i< = str.length; ++ i){
if(isVowel(i)){
++ count;
}
}
返回计数;
};

我也尝试了上述方法,但是在for()区域的str之后删除了.length。 / p>

另一个例子,但是这个给我一个错误:

  function countVowels(str){
var count = 0
var pattern = / [aeiouAEIOU] /

for(var i = 1; i <= str.length(pattern) ; ++ i){
if(isVowel(i)){
++ count;
}
}
个返回计数;
};

我也尝试了其他各种功能,但为了使这篇文章相对简短,我将不会继续发布它们。我刚接触Javascript,也不确定自己做错了什么。任何帮助将不胜感激!

解决方案

虽然Robiseb的答案是正确的方法,但我想让您知道为什么代码无法正常工作(我指您的第一次尝试)。基本上,您在循环中犯了两个错误:


  1. 正如CBroe所说,您正在传递 i isVowel 函数。 i 是一个整数,代表循环的索引,而不是字符串内的实际字符。要获取字符,您可以执行 str.substr(i,1),这意味着给我一个位置 i 在字符串中。


  2. 您没有为 i 变量赋予初始值。创建变量时,它是未定义的,因此无法递增。


  alert(countVowels ( hello));函数countVowels(str){var count = 0;对于(var i = 0; i< = str.length; ++ i){if(isVowel(str.substr(i,1))){count ++; }} return count;};函数isVowel(ch){var pattern = / [aeiouAEIOU] / return pattern.test(ch);};  






更新:您将看到其他答案使用其他方法来选择索引中字符串内的字符。实际上,您有很多选择。仅供参考:

  str.slice(i,i + 1); 
str.substring(i,i + 1);
str.substr(i,1));
str.charAt(i);
str [i];


I'm trying to write a Javascript function that counts the vowels in a string by calling another function inside that function, but when I test it in the console it returns 0.

Here is my first function that works fine and recognizes if a string is a vowel:

function isVowel(ch){
    var pattern = /[aeiouAEIOU]/
    return pattern.test(ch);
};

For the second function none of my ideas have worked. Here are a few examples of what I have tried so far:

This one returns me a 0:

function countVowels(str){
var count = 0;

for(var i; i <= str.length; ++i){
    if(isVowel(i)){
        ++count;
    }
}
return count;
};

I also tried the above, but removing the .length after str in the for() area.

Another example, but this one gives me an error:

function countVowels(str){
var count = 0
var pattern = /[aeiouAEIOU]/

for(var i = 1; i <= str.length(pattern); ++i){
    if(isVowel(i)){
        ++count;
    }
}
return count;
};

I've tried various other functions as well, but for the sake of keeping this post relatively short I won't continue to post them. I'm quite new to Javascript and I'm not sure what I'm doing wrong. Any help would be greatly appreciated!

解决方案

Although Robiseb answer is the way to go, I want to let you know why you code is not working (I'm referring your first attempt). Basically you made two mistakes in the loop:

  1. As CBroe stated, you are passing i to your isVowel function. i is a integer representing the index of the loop, not the actual character inside the string. To get the character you can do str.substr(i, 1), what means "give me one character from the position i inside the string".

  2. You are not giving a initial value to the i variable. When you create a variable, it is undefined, so you can not increment it.

alert(countVowels("hello"));

function countVowels(str) {
  var count = 0;

  for (var i = 0; i <= str.length; ++i) {
    if (isVowel(str.substr(i, 1))) {
      count++;
    }
  }
  return count;
};

function isVowel(ch) {
  var pattern = /[aeiouAEIOU]/
  return pattern.test(ch);
};


UPDATE: You will see that other answers use other methods to select the character inside the string from the index. You actually have a bunch of different options. Just for reference:

str.slice(i,i+1);
str.substring(i,i+1);
str.substr(i,1));
str.charAt(i);
str[i];

这篇关于如何计算具有两个功能的Javascript字符串中的元音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆