Created
September 7, 2013 12:47
-
-
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]).
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
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