Created
May 2, 2016 21:56
-
-
Save tmcw/742ca38f2ff2e03cf0a2f92cc0465db5 to your computer and use it in GitHub Desktop.
This file contains 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
var numbers = [1, 2, 5, 6, 7, 9, 10]; | |
var target = 6; | |
function takeWhile(arr, fn, cb) { | |
var out = []; | |
for (var i = 0; i < arr.length; i++) { | |
if (fn(arr[i], i)) out.push(arr[i]); | |
else break; | |
} | |
return out; | |
} | |
console.log(takeWhile(numbers.slice(numbers.indexOf(target) + 1), function(res, i) { | |
return res == target + i + 1; | |
}).concat(takeWhile(numbers.slice(0, numbers.indexOf(target)).reverse(), function(res, i) { | |
return res == target + i - 1; | |
}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment