Sentence Counter

Task #5 in the mini-linter project asks to count the total number of sentences. Can the following code could be used as a possible solution?

let exclamationCount = story.split(’! ‘)
let periodCount = story.split(’. ')
console.log('Sentence count: ', exclamationCount.length + periodCount.length)

The walkthrough video shows a different way of doing it and results in 12. The above method I used results in 13. Is there an error in my code?

Hello,
Taking a quick look at your code:
e.g.
“sentence1. sentence2! sentence3!”
would give you
exclamationCount = [“sentence1. sentence2”, “sentence3”]
periodCount = [“sentence1”, “sentence2. sentence3”]
4 instead of 3… hmm

currently you’re always going to be 1 over. Account for that and I think you should be fine.

The thing to understand about .split() is that even when there are no splits, the returned array has length, 1, hence the overage.

Suggest establish a way to find ALL full stops, not just the ones you see in the text. Pretend we cannot physically examine the text but must let the program do it for us. It will result in a more useful model to apply in future.

If Regular Expressions have not been introduced, they will be coming up very soon. Seems we’ve seen them used in this exercise before.

.split(regex) will search for all the characters you include in the class with only one statement, rather than four.