Created
October 1, 2019 18:44
-
-
Save kerrishotts/a3ec30918baa33110e7e9fb94659cb3f to your computer and use it in GitHub Desktop.
Dev.to Daily Challenge #80
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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