Created
March 1, 2014 23:50
-
-
Save insin/9299529 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
/** | |
* Creates an array in which the contents of the given array are interspersed | |
* with... something. If that something is a function, it will be called on each | |
* insertion. | |
*/ | |
function intersperse(array, something) { | |
if (array.length < 2) { return array } | |
var result = [], i = 0, l = array.length | |
if (typeof something == 'function') { | |
for (; i < l; i ++) { | |
if (i !== 0) { result.push(something()) } | |
result.push(array[i]) | |
} | |
} | |
else { | |
for (; i < l; i ++) { | |
if (i !== 0) { result.push(something) } | |
result.push(array[i]) | |
} | |
} | |
return result | |
} |
neopunisher
commented
Sep 21, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment