Need a more basic understanding


#1



I understand that this code outputs my name letter by letter into the array from the text. I can type it and retype it. I get it. I just don't understand why. I was hoping someone could break down the code with a little more depth.

especially at "for(var j = i; j <myName.length + i; j++)"

how does the length of the name + i incremented by 1 actually capture the name from text


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


#2

@richr759,
Read this to get an idear....
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


#3

thank you that's perfect. but i'm still confused on this part

rightIndexFingerPos < (leftIndexFingerPos + myName.length);

that means the END of the loop is rightIndexFingerPos IS LESS THAN leftIndexFingerPos + however many characters your name is. right?

so confusing
why does that get the name exactly? is it possible to break that down further?


#4

why is it LESS THAN leftIndex plus yourname? shouldn't it be EQUAL TO??


#5

@richr759
Maybe doing some reading in the Book
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length


#6

i appreciate it. i pretty much get it. just can't quite picture the whole process


#7

@richr759,
The exercise is a un-exact =search your name= code construct......

What they try to achieve
is that you learn to to use =nested= IF and FOR-loop statements......

Within the exercise you are browsing through a given text,
and as soon as you find a letter
which matches the first-letter-of-the-associated-Value-of the-myName-VARIABLE
you will collect characters from the given text

  • starting from the position of the left-finger.... but you have put your right-finger on top of your left-finger, and
    • you keep your left-finger in place
    • you start collecting characters with your right-finger (the amount being myName.length -1 as you will start counting from virtual-zero, therefor you use the less-than < operator )

The down-side of the code
For instance
text="J.J.Cale was an old man he was J.J.Cale"
myName="Jay"
As the first-character of myName is a capital J
you would get the collection

J.J
J.C
J.J
J.C

so you have NO check if the collected characters are actually your name


#8

Thank you bro that really helped. Less than cause of absolute zero. Itd probably be alot easy to understand if it was an exact search your name construct. Right. Thanks again.

But one more thing. Its j = less than name.length + i. The plus i is the actual search through text itself or the first letter in myName?


#9

@richr759,

The i

  • is the position at which your left-finger is (and is preserved/saved as you work with j )
  • is the position at which you found the character matching the 1st character of myName
  • is the starting-point of you right-finger

#10

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