Created
April 25, 2017 19:09
-
-
Save andytwoods/7fc343a8f74b82f9db6e21b044c9a525 to your computer and use it in GitHub Desktop.
interweave two arrays. Can be different sizes.
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
export default function interleave(arr1, arr2){ | |
"use strict"; | |
let combined = [] | |
let arr1_len = arr1.length | |
let arr2_len = arr2.length | |
while(arr1_len>0 || arr2_len >0){ | |
if(arr1_len>0){ | |
combined.push(arr1.shift()) | |
arr1_len-- | |
} | |
if(arr2_len>0){ | |
combined.push(arr2.shift()) | |
arr2_len-- | |
} | |
} | |
return combined | |
} | |
console.log(interleave([1,1,1,1],[2])) // [1, 2, 1, 1, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment