# I want to create mini linter using for loop but I'm stuck

Hello,

I know this exercise is for practicing iterators and array manipulation and I am done with that. But I was just playing around with the code when I found that the function ‘betterWords’ only logs the first word from the story. I’m unable to figure out any problem in my code.

``````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' ];

let storyWords = story.split(' ');
//console.log(storyWords);

let betterWords = () => {
for (let i=0; i<storyWords.length; i++) {
if (storyWords[i] !== 'extremely') {
return storyWords[i];
}
}
}

console.log(betterWords());
``````

I am having a hard time sleeping lol. Thanks in advance for helping me on seemingly a trivial problem. Also if you could come up with a better logic and solution apart from the iterators it would be very helpful. Thanks again.

1 Like

We will have covered iterators by this point, so the thing to do would be use them, instead of conventional loops.

``````let betterWords = storyWords.filter(x =>  ! unnecessaryWords.includes(x))
``````

Note that now it is an array, not a function. It should now not contain any of the words from the unnecessaryWords array.

``````storyWords.length     // 188
betterWords.length    // 182
``````
2 Likes

Thanks @mtf. I know the solution to this exercise. I was just wondering why the for loop isn’t working as expected. I wanted to know why it doesn’t log every word except ‘extremely’.

I don’t think it would be a good idea to move forward with the exercises if I’m unable to figure out a simple for loop. Thanks again.

1 Like

What does return do?
When does return get executed in your function?
When is your function done looping through the text?

2 Likes

Okay. So the for loop loops through all the words in the ‘storyWords’ array. And then if the current word is not equal to ‘extremely’ it is returned. Seems fairly simple to me but the only word that returns is the first word which is ‘Last’.

1 Like

Well yeah. If you exit at the first word, then…

Don’t stop if you don’t want to stop.

If you return from the grocery store, then you left the grocery store, you are no longer there.

The last thing a function does is to return the result.

How many times can your computer shut down? 3? One. After that it isn’t doing anything.

3 Likes

Thanks a lot. I now know I was being a little dumb there lol. In the frenzy of getting my code to work, I had forgotten that a return statement immediately stops further execution.

1 Like