Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pawarvijay
Created April 25, 2017 13:18
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 pawarvijay/60b41f9e83e8a6d0f3c5975086d10cbb to your computer and use it in GitHub Desktop.
Save pawarvijay/60b41f9e83e8a6d0f3c5975086d10cbb to your computer and use it in GitHub Desktop.
simple swap in javascript
function reverse(str)
{
console.log('INPUT ARRAY ' + str.join(''))
var r = str.length - 1, l = 0;
while (l < r)
{
swap(l, r);
l++;
r--;
}
function swap(from , to){
var temp = str[from];
str[from] = str[to];
str[to] = temp;
}
console.log('OUTPUT ARRAY ' + str.join(''))
}
var string = 'abcdefgh'
reverse(string.split(''))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment