Mini linter project / Extra tasks (last ones)

add them when encountered

have I seen this before? no? then this is the first

1 Like

Ok… Is this correct?

I know you mention that I could shorten my code but I’m not comfortable doing that atm.

What about the code’s efficiency, what do you think about it? It took me a long time to solve this! :sweat_smile:

let overusedWordsCount = {};
let eachOverusedWord = overusedWords.forEach(overusedWord => {
  overusedWordsCount[overusedWord] = 0;
});

//console.log(overusedWordsCount);

const arrayWithRemovedWords = betterWords.filter(word => {
  if(!overusedWords.includes(word)) {
    return true;
  } else {
    overusedWordsCount[word]++;
    if(overusedWordsCount[word] % 2 != 0) {
      return true;
    } else {
      return false;
    }
  }
});

Now next step… find the most frequent word.

And thanks very much for your help!

return overusedWordsCount[word] % 2 != 0

can possibly drop the comparison to 0 as well since 1 is truthy and 0 is falsy

return overusedWordsCount[word] % 2

overusedWordsCount[word]++
return overusedWordsCount[word] % 2
return overusedWordsCount[word]++ % 2

if(!overusedWords.includes(word)) {
  return true;
} else {
  return overusedWordsCount[word]++ % 2
}
return !overusedWords.includes(word) || overusedWordsCount[word]++ % 2

the != 0 part should probably be added back though, so that the return value is a bool

1 Like

You could do that, but you could also do it as part of looking it up.
Is there a count? If yes, use the count, else use 0
Then you’d write the incremented value back, so now it exists.

You can also do this:

const drop_next = new Set()
const arrayWithRemovedWords = betterWords.filter(word => {
  if (drop_next.delete(word)) {
    return false
  }
  drop_next.add(word)
  return true
})

you’d add a word to the set if you want to remember to drop it next time, and when a word is already in the set, remove it, forgetting about it again.

You could also keep counts all words regardless of whether they’re overused, and use that information to determine which word is most frequent afterwards (though, you might also want to re-count after removing words)

I get the logic… and I’d love to be able to understand it and be able to translate the logic into code but I’m definitely not ready for that yet.

Thanks for explaining it though.

About the shortening part, now it makes more sense for me when you explained the returns on truthy and falsy values…

Thanks!!