I don't like continuing my lessons until I know why everything is the way it is. So why does "i" become "j" in this line of code for finding the name


#1


text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";

var myName = "Eric";
var hits = [];

// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
	if (text[i] === "E") {
		// If we find it, add characters up to
		// the length of my name to the array
		for(var j = i; j < (myName.length + i); j++) {
			hits.push(text[j]);
		}
	}
}

if (hits.length === 0) {
	console.log("Your name wasn't found!");
} else {
	console.log(hits);
}


Replace this line with your code. 



Help with "Your second "for" loop
#2

The outer loop will iterate through 31 characters to find the first match to 'E', meaning i will be 30.

Blah blah blah blah blah blah Eric
                              ^
                         i == 30

We only wish to iterate that letter, and the next 3, and we do not want to lose our place (stored in i) so we copy i to j and use it to iterate over that segment of the text.

j = i; j < i + myName.length;

j will iterate over text[30] thru text[33].


#3

Hello.... I understand what is happening as far as j iterating over the remaining letters of my name. However..... I dont understand the actual equation itself.

why wouldnt the equation be j = i; j < myName.length;

                                           instead of     j = i; j < i + myName.length;   ??

my logic is wondering if we have already made j = i in the first statement then wouldnt the first letter of the name already be accounted for in the i for loop? and if that is true.... then the first letter of the Name would already be taken care of and would not need to be added back into

j = i; j < i + myName.length; equation? hope that is making sense???


#4
"The text contains a name, Codecone at position index 26"
 012345678901234567890123456       4
           11111111112222222       3

Match found at `i == 26`   => j = i
myName.length ==    + 8

ending index ==  j < 34

hits.push(text[j])  => ['C', 'o', 'd', 'e', 'c', 'o', 'n', 'e']

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.