2nd for loop not pushing elements in Array


#1



Please help, I am still getting this error message "Oops, 2nd for loop isnt pushing values in the array."


var text = "My name is Yusuf. My wife also calls my Yusuf, my dad calls me Yusuf.";

var myName = "Yusuf";

var hits = [];

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

Continuing the discussion from 5/7 Second For isn't Pushing Values to the Array:


#2

Got my mistake finally.

I was comparing text[0] === 'Y' in my if loop which is wrong.

It should be text[i] === 'Y'.


#3

@yusufht,
You will have to change the condition of your IF statement
from your

( text[0] === "Y" )

into

( text[i] === "Y" )

And you might want to add the code line
console.log( hits );
to see the Result of your code......


#4

This is the whole Correct_Code... :wink:

var text = "Anand is my name!";
var myName = "Anand";
var hits = [];

for(i=0;i<=text.length;i++)
{
if(text[i] === 'A')
{
for (j = i; j < myName.length + 1;j++){
hits.push(text[j]);

}

}
};