It currently finds the first most repeated word, now I need it to grab multiple if more a word is tied for being used. The part where my code fails is when I split() to separate the words then Set() to delete duplicates. I think my problem comes from switching from string to array? I just need a simple way for this to work, any suggestions are appreciated. Thank You!
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 = word
}
else if(wordValue == maxCount){
maxWord = maxWord + " " + word
maxWord = maxWord.split(" ")
maxWord = [...new Set(maxWord)]
}
})
console.log(maxWord)
}
commonWord()