Created
April 25, 2017 13:18
-
-
Save pawarvijay/60b41f9e83e8a6d0f3c5975086d10cbb to your computer and use it in GitHub Desktop.
simple swap in javascript
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 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