I’m new to javascript, I’m trying to modify the string passed as argument to function.
const message = "Reverse";
function reverseWords(message) {
let leftIndex = 0;
let rightIndex = message.length - 1;
// Walk towards the middle, from both sides
while (leftIndex < rightIndex) {
// Swap the left char and right char
const temp = message[leftIndex];
message[leftIndex] = message[rightIndex];
message[rightIndex] = temp;
leftIndex++;
rightIndex--;
}
console.log(message) //Output Reverse; I don't know why
}
reverseWords(message);
I know C language there string is store like this in memory [R,E,V,E,R,S,E, ], but I’m confused why the function isn’t updating my message array.
Please help me to visualize it