javascript

countConsonants()

Parameters: count-consonants(str: string)

input string to count consonants in

Returns: Number of consonants in the input string

This function takes a string as its input and returns the count of consonants present in the string. It uses the JavaScript built-in functions and regex to solve the problem.

Functions
Strings
Regular Expressions
Length Property
Medium dificulty

Crafting a Javascript Function to Count English Consonants

Hello there, Programmer! This post will guide you through the steps of creating a function called countConsonants in JavaScript. This function will count the number of consonants in a given string. It's essential for any programmer to understand string manipulation and character counting skills. Whether you're a veteran developer or a newbie, this function is a beneficial addition to your JavaScript toolbox. Let's dive in!

Step 1: Understand the problem

The task is to write a Javascript function countConsonants that receives a string word as parameter, and returns the number of consonants in the string.

Before we start coding, let's understand what a consonant is. In English, a consonant is any letter in the alphabet, in the lower or upper case, except for the vowels "A, E, I, O, U".

We are going to approach this problem in a very straightforward way: we will iterate over the string, check if each character is a consonant and, if it is, increment a counter.

function countConsonants(word) {
  // We will fill this code block in the next steps
}

Step 2: Initializing the counter

We need a variable to keep track of how many consonants we have found so far. Let's name this variable count and set it initially to 0.

function countConsonants(word) {
  let count = 0;
}

Step 3: Iterating over the string

We will use a for..of loop to iterate over each character in the string. For now, we will just print the character.

function countConsonants(word) {
  let count = 0;
  
  for (let char of word) {
    console.log(char);
  }
}

Step 4: Checking if character is a consonant

To check if a character is a consonant, we will use a trick: we check if it not a vowel. For this, we create a string with all vowels (in both cases) and check if the character is not in this string.

If the character is not a vowel, it is a consonant, then we increment the count variable.

function countConsonants(word) {
  let count = 0;
  const vowels = 'aeiouAEIOU';
  
  for (let char of word) {
    if (!vowels.includes(char)) {
      count++;
    }
  }
}

Step 5: Returning the count

In the previous step, we incremented the count variable each time we found a consonant in the word. The last step is to return this count as the result of the function.

function countConsonants(word) {
  let count = 0;
  const vowels = 'aeiouAEIOU';
  
  for (let char of word) {
    if (!vowels.includes(char)) {
      count++;
    }
  }
  
  return count;
}

Conclusion

We have successfully created a JavaScript function that counts the number of consonants in a given word. It starts by initializing a counter to zero, then it starts reading the word character by character. If a character is not a vowel, it is a consonant and is considered in the total count. In the end, after reading all the characters in the word, the function returns the count of consonants.

Learn function in:

Consonant Counting

Counts the amount of consonants in a string

Learn more

Mathematical principle

No specific mathematical principle is used here. However, understanding of character sets, particularly the English alphabets is required. Regular expression is used which aids in searching specific patterns in the string. `count = string.length - string.replace(/[aeiouAEIOU]/g, '').length;` This line of code removes all the vowels and then subtracts the length of the resulting string from the original string length to get the count of consonants.

Learn more