Does for...of in JS pass values or references?

35 Views Asked by At

I tried to change a value while working with this iterator and when I made a change nothing happened, I had to change it to a normal for.

This didn't work:

function spinWords(string){
  string = string.split(' ');
  for (let word of string) { //using for...of
    if (word.length >= 5) {
      let aux = [];
      for (let i=0; i<word.length; i++) {
        aux[i] = word[word.length - 1  - i];
      }
      word = aux.join('');
    }
  }
  return string.join(' ');
}

This did work:

function spinWords(string){
  string = string.split(' ');
  for (let j=0; j<string.length; j++) { //switched to for
    if (string[j].length >= 5) {
      let aux = [];
      for (let i=0; i<string[j].length; i++) {
        aux[i] = string[j][string[j].length - 1  - i];
      }
      string[j] = aux.join('');
    }
  }
  return string.join(' ');
}

Thanks!

0

There are 0 best solutions below