0

I have been very stuck on a bonus task from a tutorial, which is to remove every other word (contained in the array overusedWords) from the main passage betterWords (a long array of words making up a story).

For reference:

let overusedWords = ['really', 'very', 'basically'];

Focusing just on the word 'really', here is what I have come up with, with no success so far.

for (let word of betterWords) {  
     if (word === 'really' && word % 2 !== 0) { 
        betterWords.splice(word, 1) 
     } 
}

Any advice welcome, thank you

Ricky Sixx
  • 581
  • 1
  • 10
  • 25
mtahir2020
  • 13
  • 2
  • Hi, welcome to Stack Overflow. Could you explain better your question? What are you trying to achieve? Which words do you need to remove from `overusedWords` array? – Ricky Sixx Jan 24 '21 at 20:47
  • Hi Riccardo, all of them, but here I was just focusing on 'really'. – mtahir2020 Jan 25 '21 at 10:03
  • So you need to clear `overusedWords` array, whatever values it contains. You should check this answer: https://stackoverflow.com/a/1232046/9864539. This answer explains all the possible ways in Javascript to clear an array. – Ricky Sixx Jan 25 '21 at 10:07
  • Thanks, I will review that now – mtahir2020 Jan 25 '21 at 10:22

2 Answers2

0

Here is a possible solution:

  1. Put overusedWords items in a Set
  2. Define a Map to store word-count pairs
  3. Iterate over betterWords using .reduce, in each iteration:
    • If the word is included in overusedWords set:
      • Get count of the current word in the passage until now
      • if it's even, add it to the resulting array
      • increment the count of this word by 1
    • Else, always add it to the resulting array

In the end, you will have the betterWords array with the words of overusedWords removed every other time they appeared:

const overusedWords = ['really', 'very', 'basically'];
const betterWords = ['hello','basically', 'really', 'very', 'really', 'very', 'basically', 'very', 'really', 'basically', 'really', 'very', 'basically', 'really','bye'];
const map = new Map();
const set = new Set(overusedWords);

const res = betterWords.reduce((acc,word) => {
  if (set.has(word)) {
    const wordCount = map.get(word) || 0;
    if (wordCount%2 === 0) {
      console.log(word, wordCount);
      acc.push(word);
    }
    map.set(word, wordCount+1);
  } else {
    acc.push(word);
  }
  return acc;
}, []);

console.log(res);
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
  • Thanks, this is great. A little advanced for me at the moment but I will store for future reference and try to break it down. – mtahir2020 Jan 25 '21 at 10:20
0
for (let betterWord of betterWords) {  
         if (overusedWords.includes(betterWord)) { 
           let removedIndex = betterWords.indexOf(value);
            if (removedIndex > -1) {
              betterWords.splice(removedIndex, 1);
            }
         } 
    }
  • Thank you, this is brilliant and gives me so much clarity on moving forward. This code removed EVERY word from the passage, rather than every other word :). I have modified the code as such but still with no luck (sorry that the code is hard to read): `for (let word of betterWords) { if (overusedWords.includes(word)) { let removedIndex = betterWords.indexOf(word); if (removedIndex > -1 && removedIndex % 2 === 0) { betterWords.splice(removedIndex, 1); } } }` – mtahir2020 Jan 25 '21 at 10:20