Your second "for" loop: not pushing values to hits array


#1


". It looks like your second 'for' loop isn't pushing values to the hits array. " is my error message.
My log prints:
0
undefined
1
undefined
2
undefined
3
undefined
4
undefined
1
undefined
2
undefined
3
undefined

and continues a weird pattern of numbers and "undefined" until 92.

var text = "blah blaoop blargh AxixA blech bloppity blippity bloo blahgh blest blug bloog blur blergh"
var myName = "AxixA"
var hits = []

for (var i = 0; i < text.length; i ++)  {
    if( text[i[0]] === myName[0]);   {
       for ( var j = i; j < i + myName.length; j++)   {
           console.log(j);
           console.log(j[0]);
        }
    }
}

To be honest, I dont really understand this 2nd "for" loop, which was a solution presented in another problem(but the first to print something)
I was originally thinking of code that would match the length of characters within the string to 5, if it started with the same character"A". But couldnt figure out how to call the length of a string within the array 'text'.


#2

You're not actually pushing the matches to the array hits, which is why you're getting the error message.

Just a reminder, to push something into an array, you follow the syntax of


#3

That's the purpose of the second for loop. You shouldn't print out some value in every cycle, actually you should only print once at the very end (and after every loop) the array that you filled. And that's what the error is telling you: you are not pushing values into the hits array!
hits.push(some_text)

It should go something like this:

for (/*loop trough the text*/){
  if (/*a particular index is equal to your name's first letter*/){
    for (/*loop trough that word as many times as you name's length*/){
      //push the letter to the hits array
    }
  }
}
//print out the hits array

#4

IDK what I was smoking! Thanks, this is what happens when you code at 530am in the morning with no coffee.


#6

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