- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Frequency of vowels and consonants in JavaScript
We are required to write a JavaScript function that takes in a string which contains English alphabets. The function should return an object containing the count of vowels and consonants in the string.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const str = 'This is a sample string, will be used to collect some data'; const countAlpha = str => { return str.split('').reduce((acc, val) => { const legend = 'aeiou'; let { vowels, consonants } = acc; if(val.toLowerCase() === val.toUpperCase()){ return acc; }; if(legend.includes(val.toLowerCase())){ vowels++; }else{ consonants++; }; return { vowels, consonants }; }, { vowels: 0, consonants: 0 }); }; console.log(countAlpha(str));
Output
The output in the console will be −
{ vowels: 17, consonants: 29 }
- Related Articles
- Moving vowels and consonants using JavaScript
- Validating alternating vowels and consonants in JavaScript
- Alternating Vowels and Consonants in C/C++
- C# Program to count number of Vowels and Consonants in a string
- Arrange consonants and vowels nodes in a linked list in C++?
- How to detect vowels vs consonants in Python?
- Java Program to Count the Number of Vowels and Consonants in a Sentence
- Swift Program to Count the Number of Vowels and Consonants in a Sentence
- Haskell Program to Count the Number of Vowels and Consonants in a Sentence
- Kotlin Program to Count the Number of Vowels and Consonants in a Sentence
- How to count number of vowels and consonants in a string in C Language?
- How to Count the Number of Vowels and Consonants in a Sentence in Golang?
- Print number of words, vowels and frequency of each character
- C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String
- In the word 'GEOMETRY' find the ratio of vowels to the consonants.

Advertisements