How would you refactor this code to split and join a string?

This is code is good enough to pass on the unit tests. However, I am not satisfied with the design/approach. It seems to be particularized too much for the exercise purpose. How would you refactor this with Regular Expressions?


function sentensify(str) {
// Only change code below this line

if (str.includes(".")) {
  let arrStr = str.split(".");
  let newStr = arrStr.join(" ");
  return newStr;

}
else if (str.includes("-")) {
  let arrStr = str.split("-");
  let newStr = arrStr.join(" ");
  return newStr;
}
else {
  let arrStr = str.split(",");
  let newStr = arrStr.join(" ");
  return newStr;
}

}
console.log(sentensify("The.force.is.strong.with.this.one"));
console.log(sentensify("May-the-force-be-with-you"));
console.log(sentensify("There,has,been,an,awakening"));

Browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0.

Challenge: Combine an Array into a String Using the join Method

Link to the challenge:

Thanks in advance

Hello~!

I’ve put the code behind a spoiler so people do not unintentionally see a working solution. I created a RegEx that matches ., -, and , to split the string, and then join it with a " " whitespace. :slight_smile:

Edit2: removed the solution.

Edit: I also blurred your working solution, for the same reason I’ve blurred mine. :slight_smile:

1 Like