Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
//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'));
//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'));

atuttle commented Aug 12, 2016

I find your use of single character variable names... disturbing. ;)

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