Skip to content

Instantly share code, notes, and snippets.

@aichholzer
Last active August 29, 2015 14:26
Show Gist options
  • Save aichholzer/b1b7934cd514883eb8e5 to your computer and use it in GitHub Desktop.
Save aichholzer/b1b7934cd514883eb8e5 to your computer and use it in GitHub Desktop.
String weaver
'use strict';
module.exports = function stringWeaver(stringArray, sortByLength) {
sortByLength = sortByLength || true;
var result = [],
index = 0,
lastIndex = 0;
if (sortByLength) {
stringArray.sort(function (a, b) { return a.length - b.length; });
}
stringArray.forEach(function(string) {
string = string.split('');
string.forEach(function(char) {
if (index > 0) {
result.splice(index, 0, char);
index += (lastIndex + 1);
} else {
result.push(char);
}
});
lastIndex += 1;
index = lastIndex;
});
return result.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment