Joined July 2015
·
Posted to
Vowel Count (JS Algorithm)
over 1 year
ago
you don't need to regexp.match each character. regexp.match returns all the matches in the array, and you can get the length of the array once without a for loop.
function vowel_count(str)
{
'use strict';
if(typeof str != 'string') {
return false;
}
var count = 0;
var pattern = /[aeiou]/ig;
var matches = str.match(pattern);
return matches != null? matches.length:0;
}
Your first function returns the length of the array, so I'm not quite sure if that's what you intended; however, here is a way without a loop.