user5462898
user5462898

Reputation:

js multi-string

I'm trying to understand this code :

var text = "Hi, my name is Tyler. How are you?";
var myName = "Tyler";
var hits = [];
for (i = 0; i<= text.length; i++) {
  if (text[i] === "T") {
    for (j = i; j = (i + myName.length); j++) {
      hits.push(i);
    }
  }
}

What does text[i] actually mean? To me, it appears to be saying "find the i element inside the text array". I don't understand this for two reasons. The first one is that the i element is not defined as anything. Is it saying to search every element in the text "array" until it matches T, which is the first letter of my name? And second, why is it representing text as an array when it is just a string? Doesn't the [ ] brackets after text mean it is referring to text as an array?

I'm sorry if this question didn't really make sense, but I just feel so confused by it all! Thanks in advance for anyone who can answer this for me!

Upvotes: 0

Views: 45

Answers (3)

user5466693
user5466693

Reputation:

JavaScript strings and arrays share one thing in common: They are both iterable,

Example

Array

var myArray = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
console.log(myArray.length); // 10
console.log(myArray[myArray.length]); // undefined
console.log(myArray[myArray.length-1]); // 9

String

var myString = "0123456789";
console.log(myString.length); // 10
console.log(myString[myString.length]); // undefined
console.log(myString[myString.length -1]); // 9

Upvotes: 1

user5466458
user5466458

Reputation:

JavaScript strings and arrays share one thing in common: they are both iterable, meaning we can examine one element at a time. We refer to that element with a subscript, [ index ] where index is a number in the range 0 to length -1.

var myArray = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
console.log(myArray.length); // 10
console.log(myArray[myArray.length]); // undefined
console.log(myArray[myArray.length-1]); // 9

var myString = "0123456789";
console.log(myString.length); // 10
console.log(myString[myString.length]); // undefined
console.log(myString[myString.length -1]); // 9

We see that both have a common indexing characteristics:

They are both zero-indexed. They both use index subscripts. They are both iterable (we can loop through the elements). To show the difference we need to attempt assignments.

myArray[myArray.length] = 10;
console.log(myArray[myArray.length-1]); // 10
myString[myString.length] = "A";
console.log(myString[myString.length-1]); // undefined

To further illustrate,

myArray[5] = 55;
console.log(myArray[5]); // 55
myString[5] = "f";
console.log(myString[5]); // 5

What this shows is that while we can assign values or objects to an array element by referring to its index, we cannot do this with strings. The term that describes this is mutability. An array is mutable (we can change its elements). A string is immutable (we cannot change its characters).

So we now know what text[i] is. A character in the string text at index i, where i begins at zero, the index for the beginning of the string.

Upvotes: 3

Quentin
Quentin

Reputation: 943591

What does text[i] actually mean?

"Get the property i from text."


find the i element inside the text array

text is a string, not an array.


The first one is that the i element is not defined as anything

i is defined on the previous line:

for (i = 0; i<= text.length; i++) {

And second, why is it representing text as an array when it is just a string?

It isn't. [] is not array specific.

See Strings: Character access:

The other way (introduced in ECMAScript 5) is to treat the string as an array-like object, where individual characters correspond to a numerical index:

return 'cat'[1]; // returns "a"

Upvotes: 4

Related Questions