Find no. of vowels(a,e,i,o,u) in a string

Find no. of vowels(a,e,i,o,u) in a string

Anil Verma's photo
Anil Verma
·Apr 3, 2021·

Subscribe to my newsletter and never miss my upcoming articles

Play this article
(Javascript)

*ex. →vowelsCount(‘*hi there*’) → 3*

### Sol. 1

vowelsCount = (str) => {

let count = 0;

for (let char of str) {

if (‘aeiou’.includes(char.toLowerCase())) {

count++

}

}

console.log(count)

return count;

}

vowelsCount(‘hi there’)

Sol. 2

vowelsCount = (str) => {

let count = 0;

const matchedArray = str.match(/\[aeiou\]/gi);

if (matchedArray) {

count = matchedArray.length;

}

return count

}

console.log(vowelsCount(‘hi there’))

Happing Coding… 👏👏

 
Share this