Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return Vowels in a string in JavaScript
We are required to write a JavaScript function that takes in a string that might contain some alphabets. The function should count and return the number of vowels that exists in the string.
Example
Following is the code −
const str = 'this is a string';
const countVowels = (str = '') => {
str = str.toLowerCase();
const legend = 'aeiou';
let count = 0;
for(let i = 0; i < str.length; i++){
const el = str[i];
if(!legend.includes(el)){
continue;
};
count++;
};
return count;
};
console.log(countVowels(str));
Output
Following is the output on console −
4
Advertisements