Skip to content

Instantly share code, notes, and snippets.

@whoeverest
Forked from insin/intersperse.js
Last active March 22, 2016 15:48
Show Gist options
  • Save whoeverest/ca4d8285257e98145952 to your computer and use it in GitHub Desktop.
Save whoeverest/ca4d8285257e98145952 to your computer and use it in GitHub Desktop.
/**
* 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 = [];
for (var i = 0; i < array.length - 1; i++) {
result.push(array[i], something);
}
result.push(array[i]);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment