-1

[Beginner at Javascript] Edit: Sorry for not being clear, i was having problem understanding that we create "i" in the for loop. "text" does not have an "i", so how does text know to look for "i" as character. As far as i know, i can only access it like this, for example - text[0] or text[2].

Below is the whole code, that i did on Codecademy. What does text[i] mean.I mean how can we select i from that. text is not an array. I know we can select array positions but how does that work with strings?

/*jshint multistr:true */
var text = "My name is Jack, Jack";
var myName = "Jack";
var hits=[];
for (i=0;i<text.length;i++) {
    if(text[i]==="J") {
        for(j=i;j<myName.length +i;j++) {
            hits.push(text[j]);
        }    
    }
};
if(hits.length===0) {
    console.log("name not found");
}
else {
    console.log(hits);
}
Lavios
  • 1,169
  • 10
  • 22

4 Answers4

1

A string is actually an array of characters so you're selecting the "ith" character in that string.

See here --> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

From your edit it seems that you're having a hard time understanding what the "i" represents. It actually is a variable that is being incremented by your for loop. So your loop starts at 0 and counts by 1 until it reaches the integer length of your text string.

alacy
  • 4,972
  • 8
  • 30
  • 47
1

You can use array-like notations to read (not to set) a character of a string.

According to 15.5.5 Properties of String Instances,

String instances also have [...] a length property, and a set of enumerable properties with array index names.

The array index named properties correspond to the individual characters of the String value.

Therefore, text[i] is equivalent to text .charAt(i);

Oriol
  • 274,082
  • 63
  • 437
  • 513
0

text is a string, which is an array of characters. so text[j] means the character at position j

Edit:

Here's an example:

var text = "abcdef";
console.log(text[0]); // prints: 'a'
console.log(text[2]); // prints: 'c'
console.log(text[text.length - 1]) // prints: 'f'
Rick
  • 389
  • 3
  • 8
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – durron597 Jul 26 '15 at 17:24
  • 1
    "What does text[i] mean" ... " I know we can select array positions but how does that work with strings?". How did I not answer his question? – Rick Jul 26 '15 at 17:42
  • 1
    I agree, this answers the question. But you could extend it a bit to provide some code example, which would raise the quality of your answer (and potentially bring you more votes). – gaborous Jul 26 '15 at 18:13
  • Thanks for the feedback. I provided an example in code – Rick Jul 26 '15 at 19:11
0

That's from the concepts of the strings prototype. Maybe this official documentation will help you https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

Eduard Jacko
  • 1,974
  • 1
  • 16
  • 29