bpTheCoder
bpTheCoder

Reputation: 11

Understanding the .length property

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.

Upvotes: -1

Views: 1726

Answers (5)

Javier Silva Ortíz
Javier Silva Ortíz

Reputation: 2982

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'

Upvotes: 5

BadPiggie
BadPiggie

Reputation: 6359

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]);

Upvotes: 0

Goran Stoyanov
Goran Stoyanov

Reputation: 2311

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]);

Upvotes: 0

Monica Acha
Monica Acha

Reputation: 1086

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

Upvotes: 0

zimmerbimmer
zimmerbimmer

Reputation: 948

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.

Upvotes: 1

Related Questions