Im trying to replace multiple spaces with a single space ;_; its not working aaaa
This function receives a string. It also takes an optional character, which should default to an undrscore ('_') 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.
replaceWhitespaceWithCharacter('Hi Mum!', '•')
returns 'Hi•Mum!'
replaceWhitespaceWithCharacter(' do not
enter ')
returns '_do_not_enter_'
*/
function replaceWhitespaceWithCharacter (str, character) {
/* This function receives a string. It also takes an optional character, which should default to an undrscore ('_') 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.
replaceWhitespaceWithCharacter('Hi Mum!', '•')
returns 'Hi•Mum!'
replaceWhitespaceWithCharacter(' do not
enter ')
returns '_do_not_enter_'
*/
console.log(str)
str = str.replace(/\s*/g,' ')
console.log(str)
str = str.split('')
console.log(str)
for(var i = 0 ; i<str.length ; i++)
{
if(str[i].match(/\s/))
{
str.splice(i,1,character)
}
}
str.shift()
str = str.join('')
console.log(str)
return str
}
replaceWhitespaceWithCharacter( ' yo yo', '*')