0

Could someone please explain what the .length property is doing in the following code:

let sentenceCount = 0;
betterWords.forEach(word => {
  if (word[word.length-1] === '.' || word[word.length-1] === '!') {
    sentenceCount++;
  }
});

I understand the basic idea of what .length does, but when I try to print out word[word.length], it prints out as undefined. If I print out word[word.length-1], then I get the . and ! in the text. I'm not understanding what word[word.length-1] actually is so that when -1 is attached it gets the characters on the end.

Thank you in advance for any advice on this.

Michele Dorigatti
  • 811
  • 1
  • 9
  • 17
bpTheCoder
  • 13
  • 1
  • 2
    What is `betterWords`? – CertainPerformance Dec 01 '19 at 06:26
  • index starts with zero. So, `'xyz'.length` would be 3 and `'xyz'[3]` will be undefined – sidgate Dec 01 '19 at 06:32
  • An alternative is to use a regular expression: `if (/[.!$]/.test(word)) {/* ends with . or !*/}`. – RobG Dec 01 '19 at 07:15
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Michele Dorigatti Dec 01 '19 at 10:34
  • betterWords is: let unnecessaryWords = ['extremely', 'literally', 'actually' ]; let betterWords = storyWords.filter(word => !unnecessaryWords.includes(word)); – bpTheCoder Dec 01 '19 at 22:12
  • Thank you all for the responses. I understand what's going on in the code! – bpTheCoder Dec 01 '19 at 22:18

5 Answers5

5

JavaScript exhibits zero based indexing, that is, the first element in an array or in a string is at position 0. Therefore, an array or string of length n has elements going from position 0 to n - 1, with element at position n being undefined. This means that an array or string with n elements has the last element at n - 1, which is accessed as someArrayString[n - 1].

.length returns the length of an array or a string. Hence, the last element of an array or a string is found at someArrayString.length - 1 which is accessed as someArrayString[someArrayString.length - 1].

From the code, it can be inferred that word is a string. Therefore, the line word[word.length-1] accesses the last char (letter) in the word (although it actually accesses the last code unit but in ASCII a code unit correspond with a 1 byte ASCII char).

For example, the string var word = "JavaScript" has length 10. With J at position 0 and t at position 9. In other words, word[0] == 'J' and word[word.length - 1] == 't'

Javier Silva Ortíz
  • 2,864
  • 1
  • 12
  • 21
  • 1
    Please don't associate strings with arrays, they are very different things. Also, *length* is the number of code units in the string. Since some characters require two code units, it doesn't necessarily reflect the number of characters. – RobG Dec 01 '19 at 07:09
  • 1
    @RobG you're right. Just tried not to overwhelm the OP. I edited it, in fact, I should have written it like so from the beginning. Thanks. – Javier Silva Ortíz Dec 01 '19 at 07:27
1

Let's say your word = 'People';

word.length would return 6 which is the character number in your word.

Since arrays (in this case string because .length can be used in strings too) start from index 0, word.length-1 would give you the 5th element of your string, which is the last character of your word.

In your code, if (word[word.length-1] === '.' || word[word.length-1] === '!') checks if the last character of a word is a dot (.) or exclamation point (!) so you can count how many sentences there are in a given string.

zimmerbimmer
  • 908
  • 7
  • 24
0

word.length-1 - Returns the last index in the word

Explanation

word= 'test!';
console.log(word[word.length-1]) 

OUTPUT - !

The Length of word = 5

We need to get the last index of the word i.e word[4] as the array indexing starts from 0

Monica Acha
  • 1,076
  • 9
  • 16
0

Your .length property returns the length of the string. The string itself is a one-dimensional array with elements of the type character so using .length gives the size of the array. Arrays use zero-based indexing.

So in your case word[word.length-1] returns the last element/character of the array and word[word.length] goes beyond the bounds of the array and that's why it prints out undefined.

Here's a playground you can test it.

const string = "Just a test.";

//get the last element
console.log(string[string.length-1]);
//trying to get an element outside the bounds of the array
console.log(string[string.length]);
Goran Stoyanov
  • 2,311
  • 1
  • 21
  • 31
0

Alright, In your code I assume that betterWords is an array because you are using forEach.

And also assume the betterWords array is something like,

betterWords = ['word.','words!','hello','world'];

Length Property

The length property returns the length of array or string.

let myArray = [0,5,6,8,9];
let myString = 'hello';

console.log(myArray.length) // 5 | because myArray has total 5 elements.
console.log(myString .length) // 5 | because myString has total 5 characters.

Index

Then index tells us the position of an element in an array or a character in a string.

The index is starting from 0.

let myString = 'hello';

/*
 * h e l l o
 * - - - - -
 * 0 1 2 3 4
 */
console.log(myString[0]); // h | because h is 0 index of hello

So now you can see the length of the word hello is 5. But the last index of the word hellois4`. Which means the last index = length - 1

That's why here we are subtracting 1 from length to get the last index.

console.log(word[word.length-1]);
BadPiggie
  • 5,471
  • 1
  • 14
  • 28