Skip to content

Instantly share code, notes, and snippets.

@kerrishotts kerrishotts/80.js
Created Oct 1, 2019

Embed
What would you like to do?
Dev.to Daily Challenge #80
// my JS engine doesn't have String.prototype.matchAll just yet
// so providing my own generator to do the same thing
function *matchAll(str, re) {
let match;
do {
match = re.exec(str); // get the first match of the regular expession
if (match) yield match; // if we have one, yield it
} while (match != null); // keep going until no more matches
}
const getLongestSequence = (str, re) =>
Array.from(matchAll(str, re)) // generators are iterable
.map(([seq]) => seq) // extract the string from the regex match result
.reduce((longest, cur) =>
cur.length > longest.length ? cur : longest // keep track of longest
, "");
const getLongestVowelSequence = str =>
getLongestSequence(str, /[aeiou]+/gi); // use vowels -- g = global; i = case insensitive
const assert = (fn, expected) => {
const result = fn();
if (expected !== result) {
throw new Error(`Expected ${expected}, saw ${result}`);
}
return result;
}
assert(() => getLongestVowelSequence("codewarriors"), "io");
assert(() => getLongestVowelSequence("aeiou"), "aeiou");
assert(() => getLongestVowelSequence("abebibobu"), "a");
assert(() => getLongestVowelSequence("vwlss"), "");
assert(() => getLongestVowelSequence(""), "");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.