.push() won't work!


#1

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

The code apparently isn't working, and I can't figure out why. It says it has to do with the .push() function. Help!


#2

You have to push your text variable and replace the === on line 7 with a lesser than sign <

var text = "gggggggggJacobgggggggJacobggggggggggggggggJacobggggg"
var myName = "Jacob"
var hits = [];

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

#3

Tried out your solution and it worked! Thanks!


#4

No problem mate, glad to help