JavaScript Interview Question: Counting vowels

In this article, we have a fun algorithm challenge: vowels.js. It’s a simple problem – count the vowels in a string. Of course, there are multiple solutions to the problem, and we’ll explore two of them below.

Directions

  • Write a function that returns the number of vowels used in a string.
  • Vowels are the characters ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’.

Difficulty: Easy

This question is quite easy. It might be asked during a junior developer interview.

Solution #1

The first solution we’ll explore is an iterative way to solve this problem. It’s pretty straightforward.

We’ll declare a count variable at the top, which is what we’ll use to keep count of the vowels in the string (str). Next, we’ll want an array of the vowels, which will be used later to compare with the characters (char) in the string (str).

Rather than a traditional for loop, we’re going to use a for of loop. We think it makes the code a bit easier to read and understand.

Notice the method called on str, .toLowerCase(). It’s important not to forget this, else we might end up with an incorrect count (as ‘A’ !== ‘a’).

So, for each character in the string, search for a vowel. If there’s a match, increase the count.

The last step is to actually return the count. We must not forget that!

function vowelCount(str) {
  let count = 0;
  const vowels = ["a", "e", "i", "o", "u"];

  for (let char of str.toLowerCase()) {
    if (vowels.includes(char)) {
      count++;
    }
  }
  return count;
}

console.log(vowelCount("abc")); // 1
console.log(vowelCount("hello")); // 2

Solution #2

This solution is even more straightforward. It makes use of a regular expression, and a ternary operator. It’s storing the regular expression matches (str.match()) in the variable matches. Then it returns the count of matches if there indeed is any, or 0 if there aren’t any matches.

function vowelCount(str) {
  const matches = str.match(/[aeiou]/gi);
  return matches ? matches.length : 0;
}

console.log(vowelCount("abc")); // 1
console.log(vowelCount("hello")); // 2

Conclusion

This challenge is rather simple, but it’s always useful to explore multiple solutions to problems. Feel free to check out some of the other challenges we’ve posted.

JavaScript Interview Question – Steps.js

Three Simple Algorithms with JavaScript

Check if Strings are Palindromes with JavaScript

comments powered by Disqus

Related Posts

Creating a Real Time Chat Application with React, Node, and TailwindCSS

In this tutorial, we will show you how to build a real-time chat application using React and Vite,as well as a simple Node backend.

Read more

The Importance of Staying Active as a Software Developer

In today’s fast-paced digital world, developers often find themselves glued to their screens for extended periods. While this dedication is commendable, it comes with its own set of challenges.

Read more

JavaScript DOM Mastery: Top Interview Questions Explained

Mastering the Document Object Model (DOM) is crucial for any JavaScript developer. While many developers rely heavily on front-end frameworks, the underlying DOM concepts are still important.

Read more