5. Don't understand what's wrong with the "Seconf 'for' Loop"


#1

Hello everyone! I'm so very stuck with this one. Please help!

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

This is the error I keep getting:

"Oops, try again. There was a problem with your syntax."

i r/*jshint multistr:true */
var text = "Hey, what are you\
a rat m8? My name is Emily. I'm not Ilya\
, I am a computer robot, whos Ilya??";

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

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


#2

@the-magnetic-cat

Here, remove the space between the ++ and j because we are incrementing. It was probably an error of typo.


#3

@the-magnetic-cat,

Remove
i r at the beginning of your code
and
for code readabillity use i++ and j++


#4

Remove space to become j++

Also, you've got curly braces I find needless. Look through!

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

#5

Thank you! It helped :slight_smile:


#6

You're welcome.

Have fun coding! :smile:


#7

What does the "j" mean in this instance?


#8

j is another variable we've to loop through i.


#9

Hey guys! Can you plz figure out the problem with my code..Its as follows:
/*jshint multistr:true*/
var text,myName,hits;
text="This is Aman. I love coding. Aman loves to code.";
myName="Aman";
hits = [];
for(var i=0;i< text.length ;i++)
{
if(text[i]===name[0])
{
for (var j = i; j < i + myName.length; j++)
{
hits.push(text[j]);
}
}
}

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.


#10

@blazingaman

Have a close look at

name[0]


#11

Got it! Thanks.:slight_smile:


#12

Why does j equal i? and what does j stand for?