0

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!

fseitun
  • 11
  • 1
  • 2
  • Also see https://stackoverflow.com/questions/50840293/setting-a-variable-equal-to-another-variable/50840423#50840423 – Scott Marcus May 02 '21 at 21:50
  • Off Topic. Your code in simpler: `function spinWords(string){ return string.replace(/[^ ]{5,}/g, word => [...word].reverse().join("")) }` – Thomas May 02 '21 at 22:12
  • Thanks @Thomas ! I kind of hate Regex, ha. I guess I'll have to study it sometime... – fseitun May 02 '21 at 22:28

0 Answers0