5. Your second "for" loop


#1

Hello. My code is:

var text = "This is the story of Margarita.\
Margarita was not too shy, nor too bold..."

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

The thing is that when I submit & save it says 18. I know that it finds my name and sums the letters but I thought that this exercise would end in this result: [M, a, r, g, a, r, i, t, a, M, a, r, g, a, r, i, t, a]. What do I have to do in order to see the array?


#2

I tried your code and saw nothing wrong with it


#3

It's supposed to be like that. :smiley:

I tried my code here, you can try it out. Look, it says the number, too.

var text = "blob blob blob Ryan blob blob Ryan blob blob blob blob blob blob blob blob Ryan blob";
var myName = "Ryan";
var hits = [];

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

#4

console.log(hits); outside your for loop


#5

/*jshint multistr:true */
var text="Hey ,how are you / doing ?My name is Emily."
var myName="Emily";
var hits=[];
hits.push("Emily");

for(var i=0;i

if(myName.substring[0]===text[i]){

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



    }

}
console.log(hits[0]);

}
/*this works*/


#6

It works for me, but I can't understand this part of code

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

What does it do? Especially cannot understand meaning of hits.push (test[j]); anyone could explain understandable? Thanks.


#7

have a read of the reply to another post it may help clarify things for you:


#8

Thanks it really helped, more or less I understood, but still don't feel confident and have some misunderstandings. It's really confusing lesson. Now I feel stupid, lol


#9

lol...u and me both...sometimes i read the sentence or paragraph two or three times and still hear my inner voice shouting...give me a break man, and pour me that drink! haha.


#10

very confusing lesson, i find myself going back a few sections to try and get this particular one but its not clicking all the way for me lol.


#11

Same.. I wonder if it's a plugin issue or something. I actually came to the forum or that exact reason! It's scary when the green button says Next.. but the console isn't doing what it's supposed to.


#12

var text = "Yeshi, Yeshi, Yeshi, Yeshi";
var myName = "Yeshi";
var hits = [];
for (var i = 0; i < text.length; i ++) {
if(text[i] === "Y") {
for (var j = i; j < (myName.length + i); j ++) {
hits.push(text[j]);
}
}
};
console.log(hits);
this worked for me


#13

This is my code i keep on getting error message..

text = "This is my name which I was given";
myName = "Granville";
hits = [];

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


#14

Hey @gbarrett78,

You combined your if and for loop here, making a syntax error:


#15

my console is pushing out absolute rubbish, why is that?

var text = "Rosh";
var myName = "Rosh"
var hits = []

for ( var i=0; iif (text[0] === "R") {
for (var j = i; j < (i + myName.length); j++) {
hits.push(text[j]);
}
}
};
console.log(hits);


#16

/*jshint multistr:true */
var text = "Hello, is your name \ Ivan? Yes, I am Ivan, thanks for remembering";
var myName = "Ivan";
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]);
}
}
}

  • On the little screen to the right i get the number 4, but below I receive an error message saying "Make sure you're pushing letters to the hits array"
    Help, anyone please? :smile:

#17

A ; after the ) in your second for loop. .... j++); { ....


#19

I dont really see what is wrong here. Been thinking for almost an hour. Can someone help?

/*jshint multistr:true */
var text = "AJ is my initials. AJ is \
the short form of my name. Some of my friends call me AJ. Others \
call me Jay"
var myName = "AJ"
var hits = []
for (var i=0; i < text.length ; i++);
{
if(myName[0] === text[i]);
{
for( j = i; j < (i+myName.length); j++);
{
hits.push(myName[j]);
};
};
}
console.log(hits)


#21

var text = "This is my name which I was given";

var myName = "Granville";

hits = [];

for(var i=0; i if(text[i]==="G"){
for( j=i; j hits.push(text[j]);
}
}
}

I keep on getting this message (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.)
can someone help me


#22

Why does the page freeze everytime I add my code? Can't get past this lesson because the page freezes and kills the page. Can you please fix your buggy code, thanks