0

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()
Johnny Bravo
  • 163
  • 11
  • 1
    Put the words in an array instead of a single variable. When the total is the same as the max, push onto the array. – Barmar Jul 19 '21 at 15:08
  • 1
    Couple of small improvements to the code: pass the string as a parameter to `commonWord(s)` rather than using a global `str`, and change that function to return a result. – jarmod Jul 19 '21 at 15:35
  • Does this answer your question? [Using Javascript to find most common words in string?](https://stackoverflow.com/questions/6565333/using-javascript-to-find-most-common-words-in-string) – pilchard Jul 19 '21 at 15:37

2 Answers2

1

The easiest way in my eyes would be just using an array to begin with:

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()

My bad if anything is wrong, I am pretty new to js.

bluejambo
  • 221
  • 1
  • 11
  • Sweet simple fix, I love it. I was wondering your thought process behind maxWord.length = 0, but besides that, this works! Thank you. – Johnny Bravo Jul 20 '21 at 02:36
1

Once you have to see not only the max but a list of "maxes", storing the most frequent word so far while constructing the list is going to be helpful:

let str = "She she sells sea shells down by the sea shore boop seasalt"

let max = 0;
const ob = str.toLowerCase().split(' ').reduce((obj,cur) => {
  if (obj[cur]){
    obj[cur]++;
  } else {
    obj[cur] = 1;
  }
  if (max < obj[cur]) max = obj[cur];
  return obj}
  ,{})
console.log(ob)
const final = Object.entries(ob).filter(([k,v]) => v === max).flatMap(([k,v]) => k)
console.log(final)

  

after @Mulan sugestion of using Map:

let str = "She she sells sea shells down by the sea shore boop seasalt"

let max = 0;
const mymap = str.toLowerCase().split(' ').reduce((obj,cur) => {
  let currentVal = obj.get(cur);
  if (currentVal){
    obj.set(cur,++currentVal);
  } else {
    obj.set(cur,1);
  }
  if (max < currentVal) max = currentVal;
  return obj}
  ,new Map())

for (let [key, value] of mymap) {
  console.log(key + ' = ' + value)
}

const final = Array.from(mymap).filter(([k,v]) => v === max).flatMap(([k,v]) => k)
console.log(final)
malarres
  • 2,941
  • 1
  • 21
  • 35
  • [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) is generally a better data structure for this kind of computation – Mulan Jul 19 '21 at 15:25
  • i'm converting an array into an object. Cannot use map for that – malarres Jul 19 '21 at 15:25
  • yes, you definitely can. `Map { "she" => 2, "sells" => 1, ... }` – Mulan Jul 19 '21 at 15:26
  • sorry i misunderstood Map with map with a capital M because being first word in a sentence. Thanks for the link – malarres Jul 19 '21 at 15:28