Mini Linter project question


while i 'm working on the mini linter project, i came across the meaning " -1".
What does " -1 " means in general and in this context down below in task no. 5 in mini linter project?
Thank you in advance!

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.

let sentences = 0;
array.forEach(word => {
  if (word[word.length-1] === '.' || word[word.length-1] === '!') {

Hi @byte4466914344
with word[word.length-1] you are addressing the last item in your word array.
Given your word array has 5 items, word.length === 5. But word[5] returns undefined, because the count start at 0, therefore word[5] would be the 6th item in your array, which doesn’t exist.
Hope I understood your question correctly?

1 Like

Thank you for the quick respond . I think i know what you mean, but what does -1 stands for?

You mean in word[word.length-1], right?
I have to slightly correct myself as you’re inside a forEach method: So you are not looking for the last item in the array but for the last character in your string. But basically, it’s the same principle.
Given your word is 5 characters long:
word.length === 5
you want to know whether your last character is a letter or a period, therefore you’re addressing the last character of the string word, which would be in a string of length === 5:
word[word.length] === word[5]
therefore you have to subtract 1:

1 Like

Just to add:

it’s a confusing thing that JS lists arrays etc. from 0 but counts lengths from 1. Take the word “packs”; if you split that into an array, JS would list the letters as follows:

p, //index 0
a, //index 1
c, //index 2
k, //index 3
s //index 4

But when counting the length of something, it does the following:

p // 1
a // 2
c // 3
k // 4
s // 5
packs.length === 5

Which leads to the slightly annoying thing that looking up the last index of an array by using the length of the array gives you one more than the last index.

1 Like

Do i ve to always convert from length into array and use -1 to do that? - sorry for the many questions

Can you specify your question? What do you mean by convert into array?

its okay. I think i got i know. Thank you both for the detailed explanation and your time. :grin: