Your second "for" loop Problem


#1


Your second "for" loop
https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661#


"Oops, try again. Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length." I don't know what to do to fix my code. Please help.


var text = "His name is Darin.  Darin is in school.  Is your name Darin too?"
var myName = "Darin"
var hits = []
for (var i = 0; i < text.length; i++){
    if (text[i] === "D"){
        for (var j = i;  i + (j < MyName.length); j++){
            hits.push(text[j]);
            console.log(hits[0]);
        }
    }
}


#2

I'm guessing the problem has to do in your second for loop, where you're saying

i + (j < MyName.length);

And I'm guessing that's the problem because that doesn't make any sense to me.

Let's say you find "D". This index is 12, you want to push each following index for as long as this count is less than the length of myName. The length of myName is 5, you're already at index 12, uh oh, let's restart. Our initial starting points was i, and we're pretending we're counting up from 0 for the length of myName, so we'll reset by subtracting our initial index, i, from our new index, j.

So j - i = 0, which is the first index of myName, now we're counting each index in the string from our target point, but pretending we're just looping through myName already. On the next character, when j is 13, 12 - 13 = 1, and myName[1] is equal to "a". See how this works?

(j < myName.length); would've returned true, which is equal to the int 1. So i + 1 would equal merely 13. that's not much of a comparison, and why that made very little sense. I'm sure you just got confused.

I would just do this, see the simple comments.

var text = "His name is Darin.  Darin is in school.  Is your name Darin too?"
var myName = "Darin"
var hits = []

for (var i = 0; i < text.length; i++) {
  // Loop through string
  if (text[i] === "D") {
  // If we find "D", Lets:
    for (var j = i; (j-i) < myName.length; j++) {
        // Add each next letter for the length of myName
        console.log(j);
        hits.push(text[j]);
    }
  }
}

I'm not trying to give away the answer, I did explain it, but with a logical mess up, it really threw me off and can mess with the understanding of the script. So use the comments I added to understand each step broken down.


When i run my code, i get a number as a result. Is that ok?
5 Your 2nd for loop
#3

What do you mean by "Index 12"? I don't understand how "D" has an index of 12


#4

Okay, so I fixed my code. I no longer have an error message popping up. Instead, my code just crashes my browser.

var text = "His name is Darin. Darin is in school. Is your name Darin too?"
var myName = "Darin"
var hits = []
for (var i = 0; i < text.length; i++){
if (text[i] === "D"){
for (var j = i; j + (i < myName.length); j++){
hits.push(text[j]);
}
}
}


#5

Okay, I fixed my code actually. Thank you for the help. It was that section of code you mentioned at first. I was just really confused. Thanks again


#6

do you understand the fix?


#7

Sorry, i could have explained that better, regretfully i was assuming that. What i meant is that strings have arraylike behaviors, in that each character in a string has an index as well. You can tell because youre searching each character by an index.

text [int]; // Returns character at this index out of text.length

So i supposed that you found your first character matching "D", because thats what your script does, which wouldve been the 12 index out of text.

Glad you were able to sort it out, and i hope it made sense.


#8

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