0

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!

3
  • 1
    Good luck with "sometimes y". Commented Oct 28, 2016 at 21:50
  • Just use str.match(/[aeiou]/gi).length. Commented Oct 28, 2016 at 21:51
  • 3
    "This one returns me a 0" - of course it does, because you are only passing the loop counter i to isVowel - so you are checking whether 0 is a vowel, 1 is a vowel, etc. Numbers aren't vowels, so the result is of course 0. Commented Oct 28, 2016 at 21:58

5 Answers 5

1

Try using .match() with the g attribute on your String.

g: global
i: case insensitive

Regexp documentation

function countVowels(ch){
  return ch.match(/[aeiouy]/gi).length;
}

var str = "My string";
alert(countVowels(str)); // 2

Sign up to request clarification or add additional context in comments.

Comments

1

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];

Comments

0

i is the index, not the character. It should be:

if (isVowel(str[i])) {
    count++;
}

Also, str.length(pattern) is wrong. length is a property, not a function, so it should just be str.length.

Comments

0

You forgot to assign the value 0 to i variable

And parameter for isVowel is the character, not the index of string

Here information about the JS language: https://stackoverflow.com/tags/javascript/info

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

function countVowels(str){
var count = 0;

  // you forgot to assign the value to i variable
for(var i = 0; i < str.length; i++){

  // isVowel(str[i]), not isVowel(i)
    if(isVowel(str[i])){
        count++;
    }
}
return count;
}

console.log(countVowels('forgot'))

Comments

0

Obviously you should do it this way:

function isVowel(c){
    var lc = c.toLowerCase();
    if(lc === 'y'){
        return (Math.floor(Math.random() * 2) == 0);
    }
    return ['a','e','i','o','u'].indexOf(lc) > -1;
}
function countVowels(s){
    var i = 0;
    s.split('').each(function(c){
        if(isVowel(c)){
            i++;
        }
    });
    return i;
}
console.log(countVowels("the quick brown fox jumps over the lazy dog"));

Which, although less efficient and less useful than other answers, at least has the entertaining property of returning a different count 50% of the time, because sometimes Y.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.