0

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,\0], but I'm confused why the function isn't updating my message array.

Please help me to visualize it

John
  • 25
  • 4
  • Or just this...? https://stackoverflow.com/questions/958908/how-do-you-reverse-a-string-in-place-in-javascript – Terry Feb 12 '22 at 15:50

0 Answers0