-1

I have a string with 4 values called alpha1 "abcd" and another string with 4 values called alpha2 "abce". I want to create a function that translates a given alpha1 string into alpha2 such that: function "abcd" returns "abce" function "dddd" returns "eeee" function "bbdd" returns "bbee" Here's my attempt:

function Replace() {
Replace.replace("d", "e");
}
tu6619
  • 19
  • 1
  • 6
  • So you want a function that replaces instances of "d" in a string with "e"s? – Andy Oct 28 '15 at 12:30
  • any where would expect Replace class come from in your code? :D have you tried googling 'string replace javascript'? a hint is that first link has all the info – mikus Oct 28 '15 at 13:32
  • Possible duplicate of [Replacing all occurrences of a string in JavaScript](http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript) – Joe Kennedy Oct 28 '15 at 13:47
  • @Andy, exactly, a function that replaces instances of "d" in a string with "e"s. – tu6619 Nov 02 '15 at 10:48

1 Answers1

0

Make sure you return the changed string. The g tells the replace method to replace globally.

function replacer(str) {
  return str.replace('d', 'e', 'g');
}

replacer('abcd'); // abce

You can also use a regex to get the same result:

function replacer(str) {
  return str.replace(/d/g, 'e');
}

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
  • thanks! unfortunately, this still doesn't pass the 'function "dddd" returns "eeee"' test, it returns "eddd" instead. Any ideas? – tu6619 Oct 28 '15 at 13:38