+ 2

Why doesn’t split(“ “) work, while split(/\s+/) does?

Hello, I was solving a JavaScript coding problem on freeCodeCamp and submitted my solution (code attached in this post). Unluckily my solution hasn’t been accepted even though the output in the console is the expected one.  The solution of FCC is the same as my code, but instead of split(“ ”), they used split(/\s+/), using a regex in parenthesis. My question is: in this case technically split(“ “) should be correct, right? If not, could you kindly explain to me why? Thanks in advance. P.S. Original problem can be found at the following link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/apply-functional-programming-to-convert-strings-to-url-slugs https://code.sololearn.com/c2E9SfEicL1U/?ref=app https://code.sololearn.com/c2E9SfEicL1U/?ref=app https://code.sololearn.com/c2E9SfEicL1U/?ref=app

15th Aug 2022, 9:25 AM
krssclaire
krssclaire - avatar
2 Respostas
+ 6
krssclaire There might be more spaces between 2 words like: abc xyz so in this case split(" ") may not work because you have given a single space in split function. Just try to give more space in your code String and see effect.
15th Aug 2022, 9:42 AM
A͢J
A͢J - avatar
+ 2
A͢J Aaah yes, just tried, thank you so much!
15th Aug 2022, 9:54 AM
krssclaire
krssclaire - avatar