-2

I'm having small issues with what I'm trying to code. I need my program to change the letters in a word to certain other letters, so for example:

const p = 'Help';
console.log(p.replace('e', 'a'))
result: "Halp"

My problem with this, is that this code only allows me to change one of the letters and I would need much more than that, anyway to do a quick fix?

Pointy
  • 405,095
  • 59
  • 585
  • 614
Ferbez
  • 11
  • 1
    [`replaceAll`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll`)? – Andy Oct 09 '22 at 16:05
  • Please clarify, do you want to replace all instances of `e`, or replace other ltters in addition to `e`? – CollinD Oct 09 '22 at 16:05
  • Other letters in addition to e – Ferbez Oct 09 '22 at 16:07
  • What other letters? – Ethan Oct 09 '22 at 16:07
  • 1
    `['e', 'c', 'd'].forEach(k => word = word.replace(k, 'a'))` – Doğukan Akkaya Oct 09 '22 at 16:09
  • please add more sample input and output – Arifur Rahaman Oct 09 '22 at 16:09
  • 1
    Does this answer your question? [How do I replace all occurrences of a string in JavaScript?](https://stackoverflow.com/questions/1144783/how-do-i-replace-all-occurrences-of-a-string-in-javascript) – ASDFGerte Oct 09 '22 at 16:17
  • 1
    @ASDFGerte OP has clarified that that is _not_ what they're looking for, make sure to check out comments before throwing down those dupe votes. – CollinD Oct 09 '22 at 16:20
  • @CollinD ok, I'll retract the dupe vote. I stopped analyzing, when the question, and existing answers, were in sync. You could use https://stackoverflow.com/questions/16576983/replace-multiple-characters-in-one-replace-call but ngl, it's still not clear, what exactly is intended. – ASDFGerte Oct 09 '22 at 16:26
  • Hey guys, one moment just trying all the fixes, going to answer soon – Ferbez Oct 09 '22 at 16:40
  • 1
    hey ASDFGerte, that was exactly what I was looking for thanks a lot! – Ferbez Oct 09 '22 at 16:44

1 Answers1

-1

You can do a for loop and replace everything you want within the string.

Sergio Schirmer
  • 301
  • 3
  • 12