Skip to content

Instantly share code, notes, and snippets.

@jineeshjohn
Created April 30, 2012 05:14
Show Gist options
  • Save jineeshjohn/2555702 to your computer and use it in GitHub Desktop.
Save jineeshjohn/2555702 to your computer and use it in GitHub Desktop.
String Concat first and last , second and second last ... using JavaScript
function foo(str){
var len = str.length, i = 0,j = len, arr = [];
var i = 0,j = len-1,half = Math.floor( len/2 );
while( i<half ){
arr.push(str[i]+str[j]);
i++;
j--;
}
if( len%2 == 1)
arr.push( str.substr( half,1) );
return arr.join("");
}
console.log( foo("abcdef") );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment