I don't know what to do 5/7


#1

/*jshint multistr:true */

var text = ("a;sldkfThomasjasldkfjsald;kfsadThomasjf")

var myName = ("Thomas")

var hits = []

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

That's my code, I don't know how to use the push function exactly. Took a long break from javascript and don't remember everything.


#2

Let me go over it from the start, comparing it to my own code which works as intended.

First: you don't need var within the for loop; you can just type
for (i = 0; etc).

Second: i+=1 isn't incorrect, but you can make it shorter by typing i++ instead. It still increments i by 1, it's just shorter :smile:

Third: in your second for loop, you now have
i < myName.length
Try making this
j < i+myName.length
and after that instead of i+=i, make it j++.
You have a new for loop here and you're working with a new variable (j); you don't want to be incrementing i here, since you already do that in the first loop in order to find all the T's in the text. This second loop is for finding everything after the T until the length of your name has been reached.

Fourth: your hits.push(text[i]) is good, but switch out i for j. That way you're having it push all the letters instead of just the T!

Hope that helps :smile:


#3

/*jshint multistr:true */

var text = ("a;sldkfThomasjasldkfjsald;kfsadThomasjf")

var myName = ("Thomas")

var hits = []

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

I think I did everything you said but it still doesn't work


#4

Aah I think I see the problem. There shouldn't be a semicolon after j++.
That, and you've still got var j = i instead of just j = i.

I think that's it... at least that works for me!


#5

I've been having the same problem. This one really has me stumped! See anything wrong with the coding?

var text = " afdjgkg/askss";
var myName = "John Cockshutt";
var hits = [];

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


#6

I don't see your name in the text string.