- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Advertisements