Mini Linter project/ Replace overused words

Hi guys! I’m almost done with my Mini Linter… I’m on the last task, where i’ve to create a function which replaces overusedWords for a replacement word of my choosing.

I’ve just discovered the .replace() method and I’d like some insight on how to use it properly for this exercise…

I’ve tried this but it doesn’t seem to work…

const replaceOverusedWords = betterWords => {
  let replacementWord = 'replacement';
  for(i = 0; i < betterWords.length; i++) {
    if(overusedWords.includes(betterWords[i])) {
      betterWords.replace(betterWords[i], replacementWord);
    }
  }
  return betterWords;
};

Any suggestions?

If I’m not mistaken, JavaScript’s Array doesn’t have a .replace() method (I think .replace() is a String method). I suggest using .splice() method instead.

1 Like

that would be map, not splice

2 Likes

checking whether something is there before replacing is redundant. if it’s not, 0 replacements happen (disregarding that array has no replace method and that it wouldn’t do what you want anyway)

1 Like

Ok this works… What do you @ionatan think about it?

const replaceOverusedWords = betterWords.map(word => {
  let replacementWord = 'replacement';

    if(overusedWords.includes(word)) {
      return word = replacementWord;
    } else {
      return word;
    }
});

remove the assignments, you’re not using those variables for anything

Done

if(overusedWords.includes(word)) {
        return replacementWord;
      } else {
        return word;
      }

Thanks @ionatan

you still have this:

const a = 5
return a

do this instead:

return 5
1 Like

Hello,
I am trying to find a method for counting the most logged word in the string, yet I can’t seem to find out if this is working properly. Can anyone take a look? Thank you.

let count = {};

let compare = 0;

let mostFrequent;

(function(story) {

for (let i=0; story.length; i++) {

  let word = story[i];

  if (count[word] === undefined) {

    count[word] = 1; 

  } else {

    count[word] = count[word] + 1 ;

  }

  if (count[word] > compare) {

    compare = count[word];

    mostFrequent = story[i];

  }

}

return mostFrequent;

})(story);