JavaScript Problem Solving: Count Vowels Coding ChallengeNew
• Updated 5/22/2026 • JavaScript Problem Solvingtutorialcoding challengepractice
Challenge Goal
In this JavaScript problem solving tutorial and coding challenge, you will build a function that counts how many vowels appear in a string. This challenge helps you practice reading requirements, identifying input and output, handling edge cases, and writing clean JavaScript code.
What You Will Build
You will create a function named countVowels. The function will accept a string and return the total number of vowels found in that string.
The vowels to count are a, e, i, o, and u. Your function should count both lowercase and uppercase vowels.
Concept Overview
JavaScript problem solving becomes easier when you break a task into smaller parts before writing code. A good process is to identify the input, expected output, rules, repeated steps, and edge cases.
- Input: A string of text.
- Output: A number representing the vowel count.
- Repeated step: Check each character in the string.
- Edge cases: Empty strings, uppercase letters, and strings without vowels.
Example
countVowels("JavaScript")
// Output: 3
countVowels("HELLO")
// Output: 2
countVowels("xyz")
// Output: 0
Coding Challenge
Write a JavaScript function named countVowels that receives one string parameter named text and returns the number of vowels found in it.
Requirements
- Create a function named
countVowels. - The function must accept one parameter named
text. - The function must return a number.
- Count the vowels
a,e,i,o, andu. - Count both lowercase and uppercase vowels.
- Return
0when the string has no vowels. - Return
0when the string is empty. - Do not use any external libraries.
Input and Output
Input
A string value.
"CompileQuestHub"
Output
A number representing the total number of vowels in the string.
5
Constraints
- The input will be treated as a string.
- The function should work with uppercase and lowercase letters.
- The function should not modify the original string.
- The solution should be written in plain JavaScript.
- Keep the code readable and easy to explain.
Hints
- Create a variable to store the vowel count.
- Create a string containing the vowels:
"aeiou". - Convert the input string to lowercase using
toLowerCase(). - Loop through each character in the string.
- Use
includes()to check whether the current character is a vowel. - Increase the count when a vowel is found.
- Return the final count after the loop finishes.
Starter Code
function countVowels(text) {
// Write your solution here
}
console.log(countVowels("JavaScript")); // Expected output: 3
console.log(countVowels("HELLO")); // Expected output: 2
console.log(countVowels("xyz")); // Expected output: 0
console.log(countVowels("")); // Expected output: 0
Solution Walkthrough
Step 1: Define the function
Start by creating a function named countVowels with one parameter named text.
Step 2: Create a vowel list
Create a string containing the lowercase vowels. This makes it easy to check if a character is a vowel.
Step 3: Normalize the input
Use toLowerCase() so uppercase letters like A and lowercase letters like a can be checked the same way.
Step 4: Loop through each character
Use a loop to check every character in the string. If the character exists in the vowel list, increase the counter.
Step 5: Return the final count
After the loop finishes, return the total number of vowels found.
Final Solution
function countVowels(text) {
const vowels = "aeiou";
let count = 0;
const lowerText = text.toLowerCase();
for (let i = 0; i < lowerText.length; i++) {
if (vowels.includes(lowerText[i])) {
count++;
}
}
return count;
}
console.log(countVowels("JavaScript")); // 3
console.log(countVowels("HELLO")); // 2
console.log(countVowels("xyz")); // 0
console.log(countVowels("")); // 0
console.log(countVowels("CompileQuestHub")); // 5
Why This Works
The function checks the string one character at a time. By converting the string to lowercase first, the function only needs to compare each character with lowercase vowels. Every time a vowel is found, the counter increases by one. When the loop is done, the counter contains the final answer.
Time and Space Complexity
- Time complexity:
O(n), because the function checks each character once. - Space complexity:
O(n), becausetoLowerCase()creates a lowercase copy of the string.
Stretch Goals
- Rewrite the solution using a
for...ofloop. - Rewrite the solution using
split(),filter(), andincludes(). - Return an object with both vowel count and consonant count.
- Ignore numbers, spaces, punctuation, and symbols.
- Add input validation so non-string values return
0safely. - Write test cases for empty strings, uppercase text, lowercase text, and strings without vowels.
Quick Checklist
- Does your function return a number?
- Does it count lowercase vowels?
- Does it count uppercase vowels?
- Does it return
0for strings without vowels? - Does it return
0for an empty string? - Is the solution easy to read and explain?

