Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

javascript - Removing every other word from an array (passage of text)

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

question from:https://stackoverflow.com/questions/65875625/removing-every-other-word-from-an-array-passage-of-text

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
for (let betterWord of betterWords) {  
         if (overusedWords.includes(betterWord)) { 
           let removedIndex = betterWords.indexOf(value);
            if (removedIndex > -1) {
              betterWords.splice(removedIndex, 1);
            }
         } 
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...