-1

I am writing a word counter. It works perfectly fine until I add extra spaces between the words and ends up counting more words than there actually are in the string. I manage to remove the extra space before and after the string, but still have no luck understanding how to remove the extra space inside the string itself.

const myCounter = {
myString : 'I like bananas and   apples.           ',

countWord: function(){
    const removingSpace= this.myString.trim();
    return removingSpace.split(' ').length;
}

}

console.log(myCounter.countWord());

0 Answers0