Skip to content

Instantly share code, notes, and snippets.

@veltman
Created September 7, 2013 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veltman/6475297 to your computer and use it in GitHub Desktop.
Save veltman/6475297 to your computer and use it in GitHub Desktop.
JavaScript function for stitching two arrays with overlapping ends into a single array (e.g. [1,2,3] and [3,4,5] into [1,2,3,4,5]).
function stitch(s1,s2) {
var l1 = s1.length;
var l2 = s2.length;
if (!l1) return s2;
if (!l2) return s1;
if (s2[0] == s1[l1-1]) return s1.concat(s2.slice(1));
if (s1[0] == s2[l2-1]) return s2.concat(s1.slice(1));
s2.reverse();
if (s2[0] == s1[l1-1]) return s1.concat(s2.slice(1));
if (s1[0] == s2[l2-1]) return s2.concat(s1.slice(1));
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment