Skip to content

Instantly share code, notes, and snippets.

@marshallmurphy
Created June 5, 2020 14:14
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 marshallmurphy/361781783dcb832d05ddb1e8450ea1d7 to your computer and use it in GitHub Desktop.
Save marshallmurphy/361781783dcb832d05ddb1e8450ea1d7 to your computer and use it in GitHub Desktop.
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