5/7 Second For isn't Pushing Values to the Array


#1

var text = "Hello there this is Jon, and how are you doing today? Good because Jon is \
is good too!";
var myName = "Jon";
var hits = [ ];

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

I keep on getting an error message saying and quote "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." I'm unsure what I'm doing wrong, though I suspect it's something really simple.


2nd for loop not pushing elements in Array
#2

Hi these parts

1

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

You should put your i > text.length like that

i < text.length

2

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

Here myName.length + 1 instead of 1 you should add i


#3

/*jshint multistr:true */

text = "naiym, jonathan, ishmael, hassan, ishmael, suleiman, jonathan, ishmael"
myName = "ishmael"
hits = []

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

i looked through the adjustements you made and i still couldnt figure out where i went wrong :frowning:


#4

Hi first here

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

Change the i > text.length to i < text.length

Then here

hits.push([j])

You should push text[j]


#5

Oh geez, I was wonder what I had gotten wrong. Anyway thanks mate for assist!


#6

var text = ("Jessna", "Jessna", "Jessna");
var myName = ("Jessna");
var hits = [];

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

I get the error message It looks like your second 'for' loop isn't pushing values to the hits array. I've looked at the other replies, but i can't get mine to work. Any ideas? thanks


#7

Hi try to change your var text

From 
var text = ("Jessna", "Jessna", "Jessna");

To 
var text = "Jessna, Jessna , Jessna";

And here

if (text[1] === 'J')

Instead of 1 put i like that

if (text[i] === 'J')

#8

PLEASE HELP ME WITH THIS

var text="My name is Suyash.He is also Suyash";
var myName="Suyash";
hits=[];
for(i=0 ; i> text.length; i++)
{
if (text[i] === myName[0])
{
for (var j = i; j < i+ myName.length; j++)
{
hits.push(text[j]);
}
}
}


#9

Removing the ( ) caused a synax error, but i changed the i and put the ( ) back and it worked. thank you so much :smiley: very helpful


#10

Hi this part

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

Change the i > text.length to i < text.length
So its should be like that

for(i=0 ; i < text.length; i++)

#11

Can someone take a look for me please? Based on my review of the forums and the lessons,
the below is where I am currently stuck. My second "FOR" loop isn't working. Also, do I have the hits.push(text[j]) in the right place? Thanks.

The error message I receive is "Oops, try again. Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length."

My Code:

text = "Hey, Dany. You are so brave for trying \
to learn to code. Dany has real grit and \
determination. Go, Dany!";

var myName = "Dany";
var hits = [];
for (var i=0; i<=text.length; i++) {
if(text[i] === "D") {
for (var j=i; i + j for (var k=j; i + k for (var l=k; i = l }
}
}
}
}
hits.push(text[j])
````


#12

Hi this part

for (var j=i; i + j for (var k=j; i + k for (var l=k; i = l }

You should do it like that

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

#13

Thanks. To be clear, are you saying that I need to get rid of the following block of code because once I wrote j, I didn't need to take it further with K and l?

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


#14

Yes remove this part.
And your hits.push(text[j]) should be inside your second for loop.


#17

Still not working. My error is Oops, try again. Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.
Which I thought I eliminated that error by removing those lines...

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


#18

First your second for loop should be like that

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

Your code should gave that strutture

for ( condition){ #first for loop
    if (condition) { #if statement 
         for (condition){ #secondo for loop
             hits.push(text[j])
         } # close the second for loop
    } # close the if statement 
} # close the first for loop

#19

It worked thank you @wizmarco
So helpful and appreciated!


#20

If you're going to just hand someone the correct code, you should at least explain what the code does and what was wrong with what they did prior. You're not helping anyone by just saying, 'change it to blah'. It allows them to move forward to something more complex and still not understand the code they just moved on from. An explosion of frustration waiting to happen which may end with them giving up.


#21

where i am going wrong? please tell me someone

var text= "Hey Soni how are you?\
What are doing Soni?";
var myName = "Soni";
var hits = [];
for(var i = 0; i console.log("fine doing math" );
if ( text[i]==='S') {
for(j = i; j<( myName.length +i); j += 1); {
hits.push(text[j]);
}
}
};


#22

Hi the error come from here

for(var i = 0; i console.log("fine doing math" )

Because the for loop should have this structure

for (statement 1; statement 2; statement 3)

Just like your second for loop and in your first for loop they aske you to go through eash letter of the variable text. And here you should remove the ```;``` after your second for loop statement

for(j = i; j<( myName.length +i); j += 1);  <== this one