Your second "for" loop - Error with pushing values


#1

Okay, so, I'm completely at a loss as to what I should be doing. After looking at some help on the boards here, I was pretty sure I was doing this right, but I clearly haven't been doing this right at all. This is my current code:

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

This is the error message I have when I submit it.

Oops, try again. 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.

Been looking at this for the past half an hour now, and I'm still stumped. What am I getting wrong?


#2

your if condition is wrong. Why do you use .length in the if condition?


#3

Oh, God. I can't believe I've been overlooking this. Thanks for pointing that out - I feel dumb now.

Updated in case someone else happens to have the same slip-up I do.

var text = "Bork Shemp bork Shemp bork Shemp bork.";
var myName = "Shemp";
var hits = [];
for(var i=0; i<text.length;i++)
{
    //This is the changed condition.
    if(text[i]===myName[0])
    {
        for(var j=i; j<i+myName.length;j++)
        {
            hits.push(text[j]);
        }
    }
}

#4

Hi all,

I am having the same issue. Can you guys tell me where I am wrong?
thx

var text = ("I am Paki trying Paki my Paki best");
var myName= ("Paki");
var hits = [];

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


#5

here:

var i = text = 0

could you explain why you initiliaze your loop like this?


#6

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