Mini Linter solution for extra tasks

Dear All,

Sorry in advance for being a beginner, but I have just started coding 6 weeks ago.

I have finished the Mini Linter project and started to work on the extra tasks. I am stuck with the task that
“1. For the overused words, remove it every other time it appears.”
I have decided to do it using the .filter method. I have declared an object to store the number of overused words. My problem is that when I try to delete the 2nd or 4th instance of the word in the text, the .indexOf method always finds the first instance and deletes that one. Here is the code I did:

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey. The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.'; let overusedWords = ['really', 'very', 'basically']; let unnecessaryWords = ['extremely', 'literally', 'actually' ]; const storyWords=story.split(' '); storyWords.filter (word => {for (let i=0; i < unnecessaryWords.length;i++) { if (word === unnecessaryWords[i]) { storyWords.splice(storyWords[word],1); } } } ); const betterWords= storyWords; const overuse = { 'really': 0, 'very': 0, 'basically': 0 }; // overusedWords delete with filter method. betterWords.filter (word =>{ for (let i=0; i < overusedWords.length;i++) { if (word === overusedWords[i]) { overuse[word]++; if (overuse[word] % 2 === 0) betterWords.splice(betterWords.indexOf(word), 1); }; }; } ); // sentence count let sentenceCount=0; betterWords.filter(word=> { for (let i=0; i<word.length;i++) { if (word[i]==='.' || word[i]==='!') { sentenceCount++; } } } ); console.log(`The number of words: ${storyWords.length+1}`) console.log('The number of sentences: '+ sentenceCount); console.log('The number of overused words: ') console.log(overuse); /* for method for deleting unnecessary words. let num=0 for (let i=0; i<betterWords.length;i++) { if (betterWords[i]===overusedWords[0]) { num++; if (num % 2 === 0) { console.log(num); console.log(i); console.log(betterWords[i]); betterWords.splice(betterWords[i], 1); } }; };*/ console.log(betterWords.join(' '));

I have tried to do it with a for loop but same result. I also tried introducing a new variable for the index of the word that needs deleting but no success.

I appreciate your help and sorry, but this is my first post here.


Hi Kalman,

that’s what the indexOf method does: It returns the first item found. But you can pass a second argument: The index where it should start searching. You can do .indexOf('word', idx);

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.