Wednesday, 2 December 2015

Kata Day 8: Count Consonants


Problem Description: Write a function consonant-Count or consonant_count  that takes a string of English-language text and returns the number of consonants in the string.

Consonants are all letters used to write English excluding the vowels a, e, i, o, u.

Level: 7kyu

Link to Kata: Count Consonants

We create two strings initially- a vowel string which contains all the vowels and a cons string containing all 26 English alphabets. We also initialize a variable n to zero. 

We convert the given string to lowercase using Javascript's toLowerCase() function.

We then loop through the every element of given string and check if the element is a part of const string to avoid any special character or spaces. Then we check if the value of that element compared to vowel string is equal to -1. If it is then it proves that the element we are comparing is an English alphabet but not a vowel. In that case we increment the value of n by 1. We continue this process for every character of the input string and then after the loop we output the value of n as our consonant count.

Here is a link to the code: Kata Day 8

Please mention your suggestions or your doubts in the comment below.

Happy coding. :)  

No comments:

Post a Comment