I'm trying to figure out how to replace all the whitespaces with a character given. My code is duplicating the character when the whitespace is replaced.
This is the exercise, and my code so far:
This function receives a string. It also takes an optional character, which should default to an underscore ('_') when not given.
It should return the same string, but with all groups of whitespace (spaces, tabs and line breaks) replaced by a single instance of the character in the second argument.
function replaceWhitespaceWithCharacter (str, character) {
return str.replace(/\s/g, character);
}
Also, I don't know how to default the underscore. Could anyone give me a hand, please? Thank you in advance.