javascript

countVowels()

Parameters: string

A string from which to count the number of vowels

Returns: The number of vowels in the string

This JavaScript function, countVowels, takes a string as an argument. It then parses the string to count the number of vowels.

functions
strings
mapping
filtering
arrays
conditionals
Medium dificulty

JavaScript: Coding a Function to Count Vowels in Text

Welcome programmer! We're thrilled to have you here. We'd like to guide you in creating a simple function. In this post, you'll learn to write a 'countVowels' function in JavaScript. This function will enable you to count the number of vowels present in a string. It is a straightforward and efficient way to understand the practical aspects of string manipulation in JavaScript. So let's deep dive into it!

Step 1: Function Declaration

Start by declaring a function named countVowels. This function will take a string as an argument. This is done with the following code:

function countVowels(str) {
  // our code will go here
}

Step 2: Define the Vowels

Now, we need to define what we are searching for, in our case - the vowels. We do this by creating a variable named vowels and assigning to its value all the vowels which can be lowercase or uppercase. This can be achieved as follows:

function countVowels(str) {
  var vowels = 'aeiouAEIOU';
  // our code will continue here
}

Step 3: Initialize the Counter

Then, create a variable counter and instantiate it to zero. This counter will hold our result, how many vowels are in the given string. Like so:

function countVowels(str) {
  var vowels = 'aeiouAEIOU';
  var counter = 0;
  // our code will continue here
}

Step 4: Count the Vowels

We need to check each character in the string to see if it's a vowel. Create a for loop to iterate through each character in the string. Within the for loop, use an if statement to check if the character is in our string of vowels. If so, increase counter by 1. This will look as follows:

function countVowels(str) {
  var vowels = 'aeiouAEIOU';
  var counter = 0;
  for(var i = 0; i < str.length; i++) {
    if(vowels.indexOf(str[i]) !== -1) {
      counter += 1;
    }
  }
  // we will handle the result here
}

Step 5: Return the Result

Finally, we need to return the counter that now holds the number of vowels in the string. The final version of our function looks like this:

function countVowels(str) {
  var vowels = 'aeiouAEIOU';
  var counter = 0;
  for(var i = 0; i < str.length; i++) {
    if(vowels.indexOf(str[i]) !== -1) {
      counter += 1;
    }
  }
  return counter;
}

And that's how you write a JavaScript function to count the number of vowels in a string!

Learn function in:

Vowel Counting

Counting the number of vowels in a string

Learn more

Mathematical principle

The core principle behind the countVowels function is the concept of array mapping and filtering. The function converts the string into an array, and then uses the `filter` method to isolate only the vowels. The length of this resulting filtered array gives the number of vowels in the input string. `countVowels(s) = # [c for c in s, where c is a vowel]`.

Learn more