Created
June 5, 2020 14:14
-
-
Save marshallmurphy/361781783dcb832d05ddb1e8450ea1d7 to your computer and use it in GitHub Desktop.
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(stringOfChars) { | |
// split string into an array: 'abc'.split('') => ['a', 'b', 'c'] | |
let arrayOfChars = stringOfChars.split('') | |
let indexLeft = 0; | |
let indexRight = arrayOfChars.length - 1; | |
while (indexLeft < indexRight) { | |
// Swap characters | |
const swap = arrayOfChars[indexLeft]; | |
arrayOfChars[indexLeft] = arrayOfChars[indexRight]; | |
arrayOfChars[indexRight] = temp; | |
// Move towards middle | |
indexLeft++, indexRight--; | |
} | |
return arrayOfChars.join('') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment