Stuck with second for loop search for name


#1

Hi,
Can anybody tell me what I'm doing wrong?

var text="Hi, I'm Astrid.Astrid is my name. \
My parents must like the name Astrid,\
because they called me Astrid."
var myName= "Astrid" ;
var hits=[]
for (var i=0; i<text.length; i++) {
if (text[i]==="A"){
for (var j=i; j< (i+myName.length);j++){
hits.push(text["j"])}}}
And I think I understand most of the steps, I just don't understand the last line, the hits.push. I get that it pushes an array with adding one every time, but what does the part between brackets mean exactly?
Astrid


#2

your code works fine?

push() is just a function to append to an array. j is the variable of the second for loop which you use to find the letters of your name in text

j should be a variable, not a string


#3

Thank you for your help.
My code gives the number 24 and the notification: "Oops, try again. Make sure you're pushing letters to the hits array!" So it looks like I'm doing something wrong.

I still do not completely understand the push part. I understand what j stands for and I understand what text stands for, but I don't realy understand what happends in the last part.
Is it correct that the hits[] indicates where the push starts? And push means it just moves on by one every time? Does the part in the middle means it moves variable j through var text?
Could you explain this a little more to me? Or do you have a link to an explanation?


#4

your code is fine, if i run it, i don't get an error (except you push the wrong thing into your array

hits is just an array, push() is a function which can append elements (this case letters of your name) into the array. Push doesn't start anywhere, it simply appends to array, push can append/push multiply things to the array, but you are currently only pushing one thing (one letter at a time)


#5

Now I understand, thank you.


#6

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