Given a string like:
\"The dog has a long tail, and it is RED!\"
What kind of jQuery or JavaScript magic can be used to keep spaces to only o
I know we have to use regex, but during an interview, I was asked to do WITHOUT USING REGEX.
@slightlytyler helped me in coming with the below approach.
const testStr = "I LOVE STACKOVERFLOW LOL";
const removeSpaces = str => {
const chars = str.split('');
const nextChars = chars.reduce(
(acc, c) => {
if (c === ' ') {
const lastChar = acc[acc.length - 1];
if (lastChar === ' ') {
return acc;
}
}
return [...acc, c];
},
[],
);
const nextStr = nextChars.join('');
return nextStr
};
console.log(removeSpaces(testStr));