I am making a function in Javascript to flip the letters of a string. I am approaching it using a multiple pointer technique.
const reverseString = (string) => {
// Start at the front and swap it with the back
// Increment front decrement back
// Do this until we get to the center
let head = string.length - 1;
let tail = 0;
let result = string;
console.log(result);
while (tail < head) {
// Swap
var temp = result[head];
result[head] = result[tail];
result[tail] = temp;
tail++;
head--;
}
return result;
};
But for some reason this swapping mechanism is not correctly assigning the head to the tail and the tail to the head. When running the function I just get the original string back return meaning that the assignment in the swapping mechanism isn't working. Anyone have any clue what I could be doing wrong.