Second For Loop Not Pushing To Hits Array


#1

Hi,

I can't figure out what's wrong with my code. Every time I submit it says "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." Here is my code:

/*jshint multistr:true */
var text = "feojfowejAnikaopsfjwoAnikafjewijAnika"
var myName = "Anika"
var hits = []

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

}

}


#2

This loop will execute as long as the current value of i (when you found myName[0])which is the value put into j is less than the length of your name("Anika") +1; Now your second for loop is not pushing to hits because when the match is found that index is greater than (1 + myName.length) so the for loop terminates right away(the body is not even executed)
to fix that you need to make this condition

(i + myName.length)

#3

Thank you so much! It worked.


#4

Hi there,

I need desperately help to understand the error. Thank you.

/*jshint multistr:true */

var text = "bla bla Vadim bla bla bla bla Vadim Vadim";
var myName = "Vadim";
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)]}


#5

hits.push[text(j)] is incorrect (you mixed up brackets and parentheses).

change it to:

hits.push(text[j]);


#6

@vadima88 rememeber that whenever you open a brace ( { ) you must have a matching closing brace ( } )


#7

Thank you @cmatian. Thank you @rydan. I was exhausted and missed the error.


#8

var text = "my name is Aaron and I like to call myself Aaron \
and other people call me Aaron and my mother calls me Aaron";

var myName = "Aaron";

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]);}}}

I'm having trouble figuring out what my problem is here. an error message pops up saying that my array is not pushing the text. I read through the Q&A about this lesson but I'm stuck.


#9

Hi just remove the ; after your for loops

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

#10

/*jshint multistr:true */
var text = "My name is Shawn in here";
var myName = "Shawn";
var hits = [];
//loops by an increment of 1 to scan text length
for (var i = 0; i === text.length; i++) {
//for each increment of 1 in text
//if first array value === "S"
if (myName[0] === text["S"]) {
//if first array value === "S"
//for n === myName.length + i
for (var n = i; n === (myName.length + i); n++) {
//push to array hits
hits.push(text[n])
}
}
}

What am I doing wrong????


#11

Hi here

if (myName[0] === text["S"])

You should put It like that text[i] equal to the first letter of your name "S" like that

if ( text[i] === "S")

Also this part

for (var n = i; n === (myName.length + i); n++)

here

n === (myName.length + i)

You should put n less than (myName.length + i) like that

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

#12

HOW DID YOU DO THAT! THANK YOU!
I got far into the lesson and would have never noticed that. I do not understand why though. Do you know any books that can help me understand why that is the right way?


#13

Your welcome , Personally I don't know a book but maybe a code moderatore could help you with that .

@haxor789 or @albionsrefuge


#14

I have been trying to figure this out forever. Someone please help
/*jshint multistr:true */
var text = "Bailey hates Bailey because Bailey is evil";
var myName = "Bailey";
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]);}}}


#15

Hi remove the ; after your for loop

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

#16

Thank you. :grinning:


#17

2 posts were split to a new topic: Can anyone can help me with this?


#18

Hi rydan,please help me out.I'm stuck ... the error message that i'm getting is that the second for loop isn't pushing values to the hits array.

var text = "Do u know Levi,who?Levi!really ofcourse i know Levi";
var myName = "Levi";
var hits = [];
for(var i = 0;i < myName.length;i++){
if(text[i]=== "L"){
for(var j = i;j<(myName.length + i);j++){
hits.push(text[j]);
}
}
}


#19

that's the issue. You want to loop over your text and not your name