What is wrong with this?!?(second loop 'for'5.)


#1

okay this is what I put in but it makes it infinite(Don't ask me why I have no idea what even makes a loop infinite???) here is the code

var text = "HERBADERBADERP Trinity \
HEPABADERPADERA Trinity HERBADERBADERP \
HERBADERP Trinity HERBDERADERP Trinity.";
var myName = "Trinity";
var hits = [];

for(var i = 0; i < text.length; i++){
if (text[i] === 'T') {
for (var i = 0; i < myName.length; i++){
}
}

}

PLEASE tell me what's wrong!!


#2

We need to define a new iterator variable for the inner loop so that i remains untouched.

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

#3

okay thanks!!!!! i'll go try that!


#4

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.
apparently...do you know what is (or the lack of) causing this?


#5

Please re-post your code so we can check it.


#6

var text = "HERBADERBADERP Trinity \
HEPABADERPADERA Trinity HERBADERBADERP \
HERBADERP Trinity HERBDERADERP Trinity.";
var myName = "Trinity";
var hits = [];

for(var i = 0; i < text.length; i++){
if (text[i] === 'T') {
for(var j = i; j < i + myName.length; j++) {

trinity = [];
trinity.push('hits');
trinity[0];

}
}

}
okay so i added this and it seems to work but it says its not pushing values to the 'Hits array'. what is the hits array and how do i push it there? and it says to make sure that myName's text appears somewhere in the text variable.


#7

Replace all the above with,

hits.push(text[j]);

#8

kk thanks so much for all the help! im finally on solo flight now! :smiley:


#9

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