Last active
August 17, 2016 12:57
-
-
Save ryanguill/2765558358c706de951fbf7319aa7b3a to your computer and use it in GitHub Desktop.
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
//http://blog.adamcameron.me/2016/08/code-quiz.html | |
var firstMatch = (input, pattern) => input.find(item => item.match(pattern)); | |
console.log(firstMatch(['a', 'at', 'cat', 'scat', 'catch'], '.+at')); |
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
//NOTE! while this works, this is intended to be more of a joke, dont ever do this. | |
function f (input, pattern) { | |
var reverse = str => str.split('').reverse().join(''); | |
var reversedIndex = (l, i) => l - (i + 1); | |
var j = '\n' + input.join('\n') + '\n', | |
i = j.search(pattern); | |
if (i == -1) return undefined; | |
var end = j.indexOf('\n', i); | |
var start = reversedIndex(j.length, reverse(j).indexOf('\n', reversedIndex(j.length, end) + 1)); | |
return j.substr(start + 1, end - start - 1); | |
} | |
console.log(f(['a', 'at', 'cat', 'scat', 'catch'], '.+at')); | |
console.log(f(['a', 'at', 'cat', 'scat', 'catch'], '.+cat')); | |
console.log(f(['a', 'at', 'cat', 'scat', 'catch'], '.t')); | |
console.log(f(['a', 'at', 'cat', 'scat', 'catch'], 'a')); | |
console.log(f(['a', 'at', 'cat', 'scat', 'catch'], '.+xat')); |
Heh. I think i
and j
enjoy some ubiquity in the contexts Ryan's using em?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I find your use of single character variable names... disturbing. ;)