Mini Linter step 5

Hello, first time asking a question on here, hope I’m in the right place.

I’m having a hard time understanding the reason why my part of the below code does not work the same as the actual solution provided by the hint in step 5. I wrote the solution as follows:

let sentenceCount = 0;
storyWords.forEach(word => {
if (word[-1] === ‘.’ || word[-1] === ‘!’){
sentenceCount += 1;
}); console.log(sentenceCount);

The correct solution uses… word[word.length-1] … in the if statement:

let sentenceCount = 0;
storyWords.forEach(word => {
if (word[word.length-1] === ‘.’ || word[word.length-1] === ‘!’){
sentenceCount += 1;
}); console.log(sentenceCount);

I’ve been running it through my head for hours and can’t seem to understand why …word[-1] … doesn’t work in this situation.

how is that measured?

which situation?

put these before and after code blocks:

some code here

Do you mean it does not work or that it is not accepted by the lesson checker?

word[word.length - 1]  !==  word[-1] 

word[-1] is a method we use in Python, but not in JS.

What if we try,



I apologize for not being clear i’m still learning all the terminology. I’m trying to figure out why when I use word[-1] in my if statement my result in the console is ‘0,’ but when I use the word[word.length-1] I get the correct answer of 12. 12 being the number of sentences in the array. Is it safe to assume that because the result in the console is ‘0,’ my word[-1] is not pointing to the last character of any of the elements that are being passed in. And because of that my sentenceCount isn’t counting any of the periods or exclamation marks.

I thought that using word[-1] would point to the last character of the element being passed in.

Not sure if this cleared up anything, regardless thanks for trying to help!

that’s testable


and also, you should be able to argue for everything you write, so what’s the argument for that it would? if you aren’t be able to find documentation to back something up then it’s probably not good enough to use either

there’s also this:

i used google and made this code work:

const re = /[.!?]/;

const numOfSentences = story.split(re);

console.log(numOfSentences.length - 1);

works on this example but the problem is in a text where u have more than one char next to each other. For example “Very nice…” or “Very good!!!”. have to find solution for that