DEV Community

Babacar Diakite
Babacar Diakite

Posted on

Looping over string

Good day. I am attempting to iterate over a string and mask each word with an odd index. The following is my current approach, but I am not yet achieving the desired outcome. Could anyone kindly assist me in identifying the source of the issue?

const test =
  "Living in that house and using the pool would be like a summer vacation, not a job";
const stringLitertor = (data) => {
  data.split(" ").forEach((word, index) => {
    if ((word[index] = index % 2) !== 0) {
      word.replace(word, "****");
    }
  });
  return data;
};

console.log(stringLitertor(test));
Enter fullscreen mode Exit fullscreen mode

Top comments (0)