I passed this but I just need someone to explain the second "for" loop!


#1

var text = "Hey, How are you doing \
Jonatas? Jonatas is doing fine right now. How about Jonatas \
over there? Oh, that Jonatas? I didn't know he was there?";
var myName = "Jonatas";
var hits = [];
for (i = 0; i < text.length; i++)
{
if( text[i] === 'J') {
for( var j = i; j < ( i + myName.length); j++)
{
hits.push('Jonatas');
console.log(hits)

          }

      }

}


#2

@jonathan691,
As automation is the trial of creating a copy of the reality....

Imagine you have a piece of text

You will go over this text with your left-index-finger
and will do it character for character untill the End-of-Text
and you keep =count= by using leftIndexFingerPos

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

At every leftIndexFingerPos you =indentify= the current character
through text[ leftIndexFingerPos ]
and want to compare this current-character with "J"

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

    if ( text[leftIndexFingerPos] === 'J') {
        //IF codeblock will only be entered 
        // if text[ leftIndexFingerPos ] equals "J"
    }
}

Now if you identified the current-character
being at leftIndexFingerPos as being a "J"
you place your =rightIndexFinger= on-top of your =leftIndexFinger=

you now have the rightIndexFingerPos at the position of the leftIndexFingerPos
var rightIndexFingerPos = leftIndexFingerPos;

Now you are going over the text with your =rightIndexFinger=
and do that as long as the condition
rightIndexFingerPos is less than (leftIndexFingerPos + myName.length)
rightIndexFingerPos < (leftIndexFingerPos + myName.length);

They want you to insert
every character found at rightIndexFingerPos
into the hits Array using the push()_ Method
thus
every-character-found will be a =new Array Element=

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

    if ( text[leftIndexFingerPos] === 'J') {
        //IF codeblock will only be entered 
        // if text[ leftIndexFingerPos ] equals "J"
        for (var rightIndexFingerPos = leftIndexFingerPos;
                 rightIndexFingerPos < (leftIndexFingerPos + myName.length);
                 rightIndexFingerPos++) {
             hits.push( text[rightIndexFingerPos] );
             //internal increment of rightIndexFingerPos with 1, and check condition 2nd FOR-loop
        } //end-of-2nd-FOR-loop
    } //end-of-IF-statement
    //continue read starting with leftIndexFingerPos + 1
} //end-of-1st-FOR-loop
console.log( hits );

Reference

== the Book ==
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push


Need a more basic understanding
J < (myName.length + i) + hits.push(text[j])
Second for loop -> hits.push(text[j]); statement
Done Correctly... still don't get it!
Second For Loop
This is way over my head.. Help please?
I don't know where i went wrong
I don't really understand this loop, can someone please explain?
Can't figure out whats wrong with loop
Are there alternative ways to express text[i]?
Second For Loop Assistance!
Want help in this. How to use for loop to exit?
#3

thanks its a bit clearer now but how does " if ( text[leftIndexFingerPos] === 'J') { " transfer location information to the FOR statement? I thought the if statement is only defining a condition?

Can you correct my interpretation of the if statement which is:

If there is a position across the variable text that equals exactly 'J' enter the code block

How does it become:

If there is a position across the variable text that equals exactly 'J' take that position and assign it to a new variable.

Does scope come into play? I have a feeling I"m reading the if statement wrong which is making the rest of the block fuzzy.


#4

@akdacquel,

The IF condition was meant to be

if ( text[leftIndexFingerPos] === myName[0])

and myName[0] being the =first-character= of myName......which was "Jane"

So there is NO correlation to the i and j counter as described in your Original Post !!!!


#5

Thank you. I was so lost on why the "j=i" part of the loop and this is a great explanation for it!


#6

Fantastic explanation. Helped me debug my code. Thank you :slight_smile:


#7

Thanks. Excelent Explanation. !!


#8

i dont understand why we have (i + myName.length), would just (myName.length) work on its own?


#9

i agree this is a good explanation but like pagnito i dont get why the iterator ends at i+myName.length


#10

There is a good explanation at this link :slight_smile: