Functional Programming - Split a String into an Array Using the split Method

Functional Programming - Split a String into an Array Using the split Method
0

#1

Tell us what’s happening:
I am using the right code here but it will not pass me

Your code so far



function splitify(str) {
  // Add your code below this line
  return str.split(/\W+/gi)
  
  // Add your code above this line
}
console.log(splitify("This.is.a-sentence"));


**Link to the challenge:**
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/split-a-string-into-an-array-using-the-split-method

#2

What does the failing test say?


#3

The problem was the code wouldn’t stop running so i had to refresh the page and then it solved.


#4

This is my solution:

function splitify(str) {
  // Add your code below this line
  return str.split(/\W/);
  // Add your code above this line
}
splitify("Hello World,I-am code");

#5

Desn’t \W represent any alphanumeric character? I would have thought that this code splits the string on any alphanumeric character as opposed to any NON-alphanumeric character.

I see that your code works, I’m just confused about that part.


#6

\w is different than \W

\w is is used to find a word character (character from a-z, A-Z, 0-9, including the _ (underscore) character).

\W is is used to find a non-word character.


#7

RIP my solution… It passes, but it’s very ugly.

return str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g," ").split(" ")

#8

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, please just focus on answer the original poster’s question instead of posting a full working solution.

Thank you.


#9

Nice.
found something in StackOverflow. But there is no perfect answer as there are many languages in this planet.

I chose /[^\w\s]|_/g although it’ll eliminate characters from other languages, but is also quite hard to try to add all the punctuation symbols available.

Also have to control the extra spaces if available .replace(/\s{2,}/g, ’ ') and at last the expected .split()


#10

Can you explain what you have done here? I don’t undestand …