- 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
Vowel, other characters and consonant difference in a string JavaScript
We are required to write a function that takes in a string of definite characters and the function should return the difference between the count of vowels plus other characters and consonants in the string.
For example −
If the string is −
"HEllo World!!"
Then, we have 7 consonants, 3 vowels and 3 other characters here so the output should be −
|7 - (3+3)| = 1
Hence, the output should be 1
Let's write the code for this function −
Example
const str = 'HEllo World!!'; const findDifference = str => { const creds = str.split("").reduce((acc, val) => { let { v, c } = acc; const vowels = 'aeiou'; const ascii = val.toLowerCase().charCodeAt(); if(!vowels.includes(val.toLowerCase()) && ascii >= 97 && ascii <=122){ ++c; }else{ ++v }; return {c,v}; }, { v: 0, c: 0 }); return Math.abs(creds.c - creds.v); } console.log(findDifference(str))
Output
The output in the console will be −
1
- Related Articles
- Alternate vowel and consonant string in C++
- Alternate vowel and consonant string in C/C++?
- Print all Subsequences of String which Start with Vowel and End with Consonant in C++
- Program to find if a character is vowel or Consonant in C++
- C++ Program to Check Whether a character is Vowel or Consonant
- Distance to nearest vowel in a string - JavaScript
- Deleting the last vowel from a string in JavaScript
- Java program to find whether given character is vowel or consonant
- Java Program to Check Whether an Alphabet is Vowel or Consonant
- Finding the length of longest vowel substring in a string using JavaScript
- Regrouping characters of a string in JavaScript
- Java program to find whether given character is vowel or consonant using switch case
- Generate random string/characters in JavaScript?
- Remove characters from a string contained in another string with JavaScript?
- Vowel gaps array in JavaScript

Advertisements