Are there alternative ways to express text[i]?


#1



5. Your second for loop

I don't fully understand why " if ( text[i] === 'b') { " portion means once the text equals "b" do something. Is there some other way to say it? I get the comparison operator determining equal to "b" but not text[i]. If there isn't another way to express it maybe someone could correct my understanding of the first step which I have included in the comments below, it the first step is fine an explanation of each other line would be helpful.

for ( i = 0; i < text.length; i++) { /// in increments of 1 character as defined by i++, go from the start of a string also known as position 0, to the last position of the string as determined the length method which gets the length of the variable text.

  • if (text[i] === "b") { /// my understanding of index is a spot or location. text[0] is "M" so what is another way of saying text[i]?

  • for(var j = i; j < myName.length + i; j++) { /// Please expand on my understanding of this. All I know is that a condition is being set up that extends to the length of myName a single character at a time. What else is happening? why are we creating a variable?

  • hits.push(text[j]); /// ? Same issue, can't understand (text[j]). I get that a variable evaluates to an empty array before the push occurs. After the push hits will be assigned a new value. What else is happening here?
    }
    }
    }


var text = "Hi my name is bob";

var myName = "bob";

var hits = [];

for ( i = 0; i < text.length; i++) {
    if (text[i] === "b") {
        for(var j = i; j < myName.length + i; j++) {
            hits.push(text[j]);
        }
    }
}


#2

Here I am trying to understand i and j from the original code. What am I misinterpreting about i and j that would cause me to even take this approach.

for(var i = 0; i < text.length; i++) {
if (text[i] === "E") {
var j = i
console.log(j);
}


#3

@akdacquel,
Maybe reading a piece in a newspaper using
https://discuss.codecademy.com/t/i-passed-this-but-i-just-need-someone-to-explain-the-second-for-loop/30895/2?u=leonhard.wettengmx.n


#4

if(text[i]==="b")

means whenever the condition will be true loop will run
it will check like this

first text[i] means text[0]==="H"
second text[i] means text[1]==="i"
third text[i] means text[2]===" "
continue..................

so when it will be text[14] the loop will run
now next for loop
you need a varible you cant use same varible for this condition
because you don't know when it'll be true
if you'll take i=0 and i < myName.length; i++ it'll be wrong/infinte loop
i can't tell you more for this.
next..

var j=i means when you are adding the value of i to j
j < myName.length+i means
if text[14]==="b" then
j=14;14<3+14;j++;//true(added b)
j=15;15<3+14;j++;//true(added o)
j=16;16<3+14;j++;//true(added b)
j=16;17<3+14;j++;//false
................................
now hits.push
you have taken a variable named hits
and that is an array

so when you use hits.push
text[j] added like this
text[14] first added "b"
text[15] second "o"
text[16 ] third "b"

hope this will help

but it'll print undefined twice.like
[ 'b', 'o', 'b', 'b', undefined, undefined ]
do you know why?