Count consonants in a string javascript
The main problem in counts lies within your conditions. Show You're increasing the number of consonants whenever one of the conditions fail (that's what Change the As others pointed out, you're also likely to run into an error as the max value of i should be Length-1. There also other problems you need to consider:
For a beginner, this may not be relevant, but it's well worth getting these techniques under your skin: It's more readable to create an array that contains all of the value you're verifying for ["a","e", etc] and then, for each char in the source string, just verify if array.indexOf(str[i]) >= 0 (which means that the character is included in the array). View Discussion Improve Article Save Article View Discussion Improve Article Save Article Given a string, count total number of consonants in it. A consonant is a English alphabet character that is not vowel (a, e, i, o and u). Examples of constants are b, c, d, f, g, .. Examples : Input : abc de Output : 3 There are three consonants b, c and d. Input : geeksforgeeks portal Output : 12 1. Iterative Method C++
Java
Python3
C#
PHP
Javascript
Time Complexity: O(n),
where n is the length of the string 2. Recursive Method C++
Java
Python3
C#
Javascript
Time Complexity: O(n), where n is the length of the string Illustration of recursive
method: How do you calculate the number of vowels and consonants in a string JavaScript?var userData = prompt ("Enter any text here"); var a = 0; var e = 0; var i = 0; var o = 0; var u = 0; var consonants = 0; var count; for (count = 0; count <= userData. legth; count++){ if((userData. charAt(count). match(/[aeiou]/))){ a++; e++; i++; o++; u++; }else if((userData.
How do you count the number of consonants in a string in Java?To count the number of consonants in a given sentence:. Read a sentence from the user.. Create a variable (count) initialize it with 0;. Compare each character in the sentence with the characters {'a', 'e', 'i', 'o', 'u' } If match doesn't occurs increment the count.. Finally print count.. How do I count strings in JavaScript?In JavaScript, we can count the string occurrence in a string by counting the number of times the string present in the string. JavaScript provides a function match(), which is used to generate all the occurrences of a string in an array.
How do you count the number of vowels and consonants in a string?#include int main(). //Counter variable to store the count of vowels and consonant.. int i, vCount = 0, cCount = 0;. char str[] = "This is a really simple sentence";. for(i = 0; i < strlen(str); i++){. str[i] = tolower(str[i]);. |