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)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay