Last active
August 29, 2015 14:09
-
-
Save shannonmoeller/409c18f6a07c2d2c0ce5 to your computer and use it in GitHub Desktop.
sprinkle
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 a = [ | |
'a', 'aa', 'aaa', 'aaaa', 'aaaaa', | |
'b', 'bb', 'bbb', 'bbbb', 'bbbbb', | |
'c', 'cc', 'ccc', 'cccc', 'ccccc' | |
]; | |
var b = [ | |
'Z', 'ZZ', 'ZZZ' | |
]; | |
function randomInt(min, max) { | |
return min + Math.floor((max - min) * Math.random()); | |
} | |
function sprinkle(a, b) { | |
// clone arrays | |
a = a && a.slice() || []; | |
b = b && b.slice() || []; | |
if (!a.length) { | |
return b; | |
} | |
if (!b.length) { | |
return a; | |
} | |
var chunk; | |
var shortArr = a.length <= b.length ? a : b; | |
var longArr = a.length <= b.length ? b : a; | |
var interval = Math.ceil(longArr.length / shortArr.length); | |
var retval = []; | |
while (longArr.length) { | |
// pop() multiple | |
chunk = longArr.splice(-interval); | |
if (shortArr.length) { | |
// inject a short item into a range of long items | |
chunk.splice(randomInt(0, chunk.length), 0, shortArr.pop()); | |
} | |
// add result to return value | |
retval.unshift.apply(retval, chunk); | |
} | |
return retval; | |
} | |
console.log(sprinkle(a, b)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment