I found this code in StackOverflow which takes a string and finds the most used word in said string.
However it does not omit spaces as a common word. I have no idea how, but how could this code be revised inorder for it to not include the spaces as a character?
I was thinking that I could write a if-statment that would omit the space as a common word. But I have no idea on how to write it?
This is the code below:
let str = "She she sells sea shells down by the sea shore boop seasalt"
function commonWord() {
if (str.length === 0) {
return null
}
str = str.toLowerCase()
let maxCount = 0
let maxWord = []
str = str.split(" ")
str.forEach(word => {
let wordValue = str.filter(w => w === word).length
if (wordValue > maxCount) {
maxCount = wordValue
maxWord.length = 0
maxWord.push(word)
} else if (wordValue == maxCount) {
maxWord.push(word)
}
})
console.log(maxWord)
}
commonWord()Any help would be greatly appriciated!
Aucun commentaire:
Enregistrer un commentaire