Second for loop


#1


https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661#


Can someone please explain why im getting this error

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.


/*jshint multistr:true */
var text = "John gndcipnc nhsgc John mcdksowie mkdift"
var myName = "John"
var hits = []

for(var i = 0; i >= text.length; i++)
{

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


#2

Hi this line

for(var j = i; j <= (i+ myName.length); i++)

you should change the i++ with j++


#3

I've tried that but the same error is coming up.


#4

then this line

for(var i = 0; i >= text.length; i++)

change the >= with <=.. Becasue i is equale to 0 so it will never be great than text.length


#5

This isn't working either. Maybe its to do with the text in the text array?


#6

can you post your new code?


#7

Sorry yeah that was right. I was changing code on the wrong line. Thanks for your help.


#8

I've got the same problem..

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.

/*jshint multistr:true */
var text = "Hey, how are you \
doing? My name is Emily.";

var myName ="Dani";
var hits = [];

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

    }
}

}

I don't see where the error is ...


#9

Try changing myName to Emily and change the if statement to === "E"


#10

This is how mine looked when it worked.

/*jshint multistr:true */
var text = "John gndcipnc nhsgc John mcdksowie mkdift"
var myName = "John"
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(text[j]);
}
}
}
if(hits.length === 0){
console.log("Your name wasn't found");
}
else{
console.log(hits);
}


#11

Thank you very much !!! Just changing the name to Emily works perfect....


#12

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