5. Your second "for" loop


#1



What's wrong with my code?

It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.


/*jshint multistr:true */
var text="Beny likes working out. Beny wants to live a healthy lifestyle. Beny is tall. Ball";
var myName="Beny";
var hits=[];
for (var i=0; i<text.length[i]; i++) {
    if (text[i]==="B") {
        for (var j=i; j<i+myName.length; j++) {
            hits.push(text[j]);
        }
    }
}


#2

That error message is making a claim about an array, is there a way you could verify that claim?
What was the program supposed to be doing with the array?
Is there a way you can find out if it's doing those things?

You could have a look at what it's doing by making it print out each step it carries out, something like:

current character is B, does it match? true
current character is e, does it match? false
current character is n, does it match? false
current character is y, does it match? false
current character is  , does it match? false
current character is l, does it match? false
current character is i, does it match? false
current character is k, does it match? false
current character is e, does it match? false
current character is s, does it match? false
current character is  , does it match? false
current character is w, does it match? false
current character is o, does it match? false
current character is r, does it match? false
current character is k, does it match? false
current character is i, does it match? false
current character is n, does it match? false
current character is g, does it match? false
current character is  , does it match? false
current character is o, does it match? false
current character is u, does it match? false
current character is t, does it match? false
current character is ., does it match? false
current character is  , does it match? false
current character is B, does it match? true
current character is e, does it match? false
current character is n, does it match? false
current character is y, does it match? false
current character is  , does it match? false
...

#3

From what I understand, the program pushes the next "myName.length" characters into "hits" array after it finds myName[0].
This is why I wrote "Ball" at the end of the text, I wanted to see if the program would push "Ball" into the hits array. ("Ball" and "Beny" both have the same first letter)


#4

Sure. But is it doing that?
A little bit of printing and you can find out.

You need to find out which part isn't doing its job, find out what isn't being done. So you'd consider the things it needs to do and then start somewhere in the middle and find out if things seem okay at that point or not. Based on that you'd then have a look earlier or later in the process, similar to looking up a name in a phone book.


#5

The condition evaluates as true, so it moves forward to the 2nd for loop. There it pushes the characters from "i" to "i+myName.length" into the "hits" array. What am I missing here?

And another thing: how dow I print out each step? Using console.log before the first for loop?


#6

console.log prints when called, yes. So you would call it each time you want something printed.

The text I generated has nothing to do with your code other (because your code doesn't print that, that's something I wrote)
It's merely an example of generating some kind of log of what it's doing.
Look at the problem you're having and ask yourself why, if the code was correct, it wouldn't have this result. And then take a look at that part of the code.

So for example if after having verified that nothing gets pushed into the array (have you done that? if not, start there) - you would then say "if it was appending letters to the array then it wouldn't be empty" and you could then add prints near the code that appends to the array and then repeat the process from there


#7

I don't think you need to have the text.length[i].
cause then before that you would need call upon the array of text, but you gave text as a string in the exercise.


#9

I just started comparing my code with the example in the lesson "1. What you'll be building?" and noticed that in my first loop the end condition was "i<myName.length[i]". Changed it and now it works.
Thanks anyway ! :grin:


#10

Will someone explain what this line does in a simpler way? Thanks!


#11

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