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,

word.slice(-1)

?

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

console.log('blah'[-1])

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:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith