0

Alright I cant seem to get this to work. I have one function written so far. It works great. Now I need to get my new function to call the second function.

Here is the first function:

function isVowelR(str) {

 if(str = str.match(/[aeiou]/gi))
    return true
 else
   return false
}

This one works fine. Returns true if str is a vowel. Here is the one Im stuck on though. This is what Ive tried (as well as some other stuff.

function countVowels(str) {
    var count = 0
    for (var i = 0; i == str.length; i++)
    {
        if (i == isVowelR(i))
        {
            ++count
        }
    }

    return count

}

This second function needs count how many vowels are in the string that is entered (which I figured out in a different function). But how do I get it to work if I am required to call the first function?

6
  • You need to check against str[i], not i alone since that is the index and not the vowel. Commented Oct 28, 2016 at 23:08
  • 1
    stackoverflow.com/q/40313624/1427878 - amazing coincidence, john-ali ... Commented Oct 28, 2016 at 23:10
  • @CBroe, I agree, however I don't think the question was "how to do it?" but more of "why my code doesn't work?" :) Commented Oct 28, 2016 at 23:12
  • @Dekel he could have asked that in the existing question, instead of creating another account. Commented Oct 28, 2016 at 23:14
  • @CBroe, you'r right. I missed the open time of the other question. probably the exact same user... Commented Oct 28, 2016 at 23:15

1 Answer 1

5

The problem is that the i variable is a number, so what you actually do is isVowelR(0), isVowelR(1), isVowelR(2) ...

You can change this to:

isVowelR(str[i])

Another option is just to check the length of the match:

str.match(/[aeiou]/gi).length

This way you don't need to loop over each character on the string:

function countVowels(str) {
    return str.match(/[aeiou]/gi).length
}
Sign up to request clarification or add additional context in comments.

2 Comments

Damn, sorry. Didn't see the other question opened 1 hour ago :\
yeah so i have the last one you commented, with the str.match as another function. That one doesnt require calling the isVowelR function. But is there a way to include the isVowelR function inside of the countVowels with the str.match portion?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.