# Mini Linter Project - Sentence Count (Help!)

Hello all!

I’m working on the Mini Linter Project and having some trouble with finding the sentences within the story.

Here’s the story:

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.’;

And here are the instructions for finding how many sentences are in this story:

Now, count how many sentences are in the paragraph.
This may seem tricky, but remember that all of the sentences in this paragraph end with a period ( `.` ) or an exclamation mark ( `!` ). You could iterate over the array and add `1` to a sentence counter variable for each word that has a period or exclamation mark as its final character.

This is what I came up with:

let senCount = 0;
storyWords.forEach(sen=> {
if (sen[sen.lengh-1] === ‘.’ || sen[sen.length-1] === ‘!’) {
return senCount++
}
});

But it will only count 3 sentences and, obviously, there are many more than that in the paragraph. Can any one tell me what’s going wrong here?

Sentences are terminated with one of four full stop characters… `.`, `:`, `!`, `?`. Note that all of them have a dot. Mind in this story there are no question marks or colons.

The problem in your code is the return within the `.forEach()`. It will stop after one sentence. This is not how to implement that particular iterator which is intentionally set up to iterate its array from end to end with no break (or return). Take out the `return`.

Truth is, I wouldn’t have chosen that method at all. We have `Array.split()` that we can implement on the full stop characters. Following is a simple regex example we could employ in a single step with no iteration. For best results, work with the original `story`, not `storyWords`.

``````const sentenceCount = story.split(/[.:?!]/).length - 1
``````

Why minus 1?

``````s = '.:?!.:?!.:?!'
console.log(s.length)
// 12
s = s.split(/[.:?!]/)
console.log(s)
//  ['', '', '', '', '', '', '', '', '', '', '', '', '']
console.log(s.length)
// 13
``````

Oh wow thank you!

I tried using .split() as well but it wouldn’t count above 0. Could that have been because of the return as well?

Edit I just tried getting rid of the return and it still only went up to 3

1 Like

`story` has an unescaped apostrophe. Should be, `it\'s`.

A better variable name would be one that describes the object, `word` comes to mind.

``````iterable[iterable.length - 1]
``````

We can refine that using the `String.slice()` method…

``````word.slice(-1)
``````
``````senCount = 0;
storyWords.forEach(word => {
if (word.slice(-1) === '.' || word.slice(-1) === '!') {
senCount++;
}
})
``````
``````console.log(senCount)    //  12
``````
1 Like