Search Text for Your Name further explanation needed


Hi guys, I know this is a noob thing to ask. But could you explain to me what happens exactly in this part of the code? On the For Loop Topic

/*jshint multistr:true */

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”) { -----what is text[i]?
// 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++) { ----- could you give me a super simple explanation how this block works?

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

Help would be appreciated! Thanks!

5/7 Second For isn't Pushing Values to the Array
Console logs first letter of myName
Second for loop

Hope this helps rather than confusing you more :stuck_out_tongue:

if (text[i] === “E”)

text[] is referring to "text = “Blah blah blah blah blah blah Eric…”; "

text[i] is referring to an index position within "text = “Blah blah blah blah blah blah Eric…”; " where “i” is initially 0 from: “for(var i = 0; i < text.length; i++) {” therefore when first run text[i] = text[0] = B from “Blah blah blah…”

B l a h blah blah blah blah blah Eric";
0 1 2 3 …

For this part i’ll try and make this as simple as I can but…

for(var j = i; j < (myName.length + i); j++) {

When this line is run the very first time, it sets the new variable “j” equal to “i” therefore if i = 0, j = 0, when it loops and i = 1, j = 1

Next you want the loop to stop when “j” is equal to the number of letters in the name. Since “j” is equal to “i”, when the first loop hits the first “E” its index is 30 (count) so now i = 30, j = 30. Now in order for the loop to stop AND include the last letter of Eric

E = index 30, r = 31, i = 32, c = 33

i = 30, myName.length = 4. Therefore (myName.length + i) = 34 and because the condition is j < (less than) and not j <= (less than or equal to) the loop stops at index 33 which is the last letter of the name Eric.

j = 33, myName.length = 4, i = 30
j < (myName.length + i)
33 < (4 + 30)
33 < 34

Loop stops, then the first loop repeats to find any other instances of the letter “E” and runs the second loop to complete the name. Rinse and repeat until no more “E’s” are found in the “text” string.

5/7 no idea what's wrong!
Second 'for' loop keeps crashing browser


If you look at the preceding line, you created the variable i which is the counter that will iterate as long as its value (variable i ) won’t be equal or superior to text.length. If the current value of the variable i in the text (which is a variable you already created ) is equal to “E” (which is the first letter of the name Eric) then will execute the code below. Let me explain:

In the course you must have certainly learned about accessing to an index in a variable like this:

var index = "String"; //We create the variable 'index'
index[0]; //We access to the first character inside the variable

It will print “S” since the index starts by 0 and first letter is considered as index 0. Now, let’s say the counter is equal to 3 (I mean the variable i), then it will go in index 3 inside your text variable (which is currently h). If it meets the “E” inside your text variable, it will execute the code below to conclude.

First, we create a variable j which is equal to variable i. As long as variable j is smaller than the length of myName variable plus the variable i (it should normally be an integer), it will execute the code below and increment variable j. Now looking back above:

We know that we created a variable named hits which has like value an empty array. In our for loop we use the push method in the hits variable which will add to this array the characters indexed to the value of variable j (which is the counter created in the for loop). Let’s say the value of variable j is equal to 7. It will add to the hits array the character found in variable text, indexed 7. In this case, it will add a to the array.

Hope this helped you!

P.-S. I noticed you copied this code from someone else but it doesn’t matter as long as you understand what you’re doing.

What is wrong?

That’s actually the pre-existing code when you start the “Search Text for Your Name” lesson 1: What you’ll be building. :stuck_out_tongue:


Lol I didn’t notice that when I was doing this particular chapter but I saw someone paste this code in the forum.


Thank you very much for the responses! I understand how it works now. :grin:

6.log it