Pronouncing the Xs [Edabit]

Edabit

Create a function that replaces all the x’s in the string in the following ways:

Replace all x’s with “cks” UNLESS:

  • The word begins with “x”, therefore replace it with “z”.
  • The word is just the letter “x”, therefore replace it with “ecks”.

Examples

xPronounce("Inside the box was a xylophone") ➞ "Inside the bocks was a zylophone"

xPronounce("The x ray is excellent") ➞ "The ecks ray is eckscellent"

xPronounce("OMG x box unboxing video x D") ➞ "OMG ecks bocks unbocksing video ecks D"

Notes

  • All x’s are lowercase.
  • I know that not all words with x’s follow this rule, but there are too many edge cases to count!

Solution:

function xPronounce(sentence) {
  let arr = sentence.split(" ").map((word) => {
    if (word.length == 1 && word == "x") {
      return "ecks";
    } else if (word[0] == "x") {
      word = "z" + word.slice(1);
    }
    return word.replace(/[x]/g, "cks");
  });
  return arr.join(" ");
}

Leave a comment