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('')
}
view raw inplace-reversal.js hosted with ❤ by GitHub