Skip to content

Instantly share code, notes, and snippets.

@felipernb
Created August 16, 2011 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipernb/1148717 to your computer and use it in GitHub Desktop.
Save felipernb/1148717 to your computer and use it in GitHub Desktop.
Given an array, normalize it to remove recursive arrays and return the number of distinct vowel in the word that has more distinct vowels
var merge_recursive = function(a, res) {
if (!Array.isArray(res)) res = [];
// Using BFS algorithm as if it was a tree
for (var i = 0; i < a.length; i++) {
if (Array.isArray(a[i])) {
merge_recursive(a[i], res);
} else {
res.push(a[i]);
}
}
return res;
}
var vowel_max_counter = function(arr) {
var tokens = merge_recursive(arr);
var count_different_vowels = function(str) {
var vowel_variations = [/[aáãàâä]/, /[eéêèë]/, /[iíîìï]/,
/[oóõòôö]/, /[uúùûü]/];
var different_vowels = 0;
for (var i = 0; i < vowel_variations.length; i++) {
if (str.match(vowel_variations[i])) {
different_vowels++;
}
}
return different_vowels;
};
var max = 0;
var different_vowels;
for (var i = 0; i < tokens.length; i++) {
max = Math.max(count_different_vowels(tokens[i]), max);
}
return max;
}
console.info(vowel_max_counter(["hola", ["soy", ["juan", "fernandez"] ], "y", ["no", "tengo", ["dinero"] ] ]));
@joseanpg
Copy link

En http://jsperf.com/gejs-estadistica-de-vocales/4 podemos apreciar lo inconveniente que resulta utilizar RegExp.prototype.exec si lo único que queremos realizar es una comprobación de matching y no nos interesa ningún detalle de éste. En estos casos sin duda, lo que hay que utilizar es RegExp.prototype.test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment