Your second "for" loop / keeps crashing


#1

It keeps crashing each time i run it. Can't figure out where im going wrong. Thank you in advance for assistance.

var text = "Hello, my name is chris. chris wrote this program. yay for chris"
var myName = "chris"
var hits = []

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

 }

};
console.log(hits);


#2

you use j as the counting variable, but you increase i.


#3

ah. thank you so much.


#4

I'm having the same problem and it's repeatedly crashing my browser. Here's my code:

var text = "alsdkfjl;askdjfl;a Jeff lsa;djla;skdfj.";
var myName = "Jeff";
var hits = []

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


#5

Yes this is the same problem: for(j = i; j < (myName.length + i); j++) {


#6

Thank you very much. Also, what do I do with the curly braces and the console.log(hits);. even withe the j++ my browser is still crashing.


#7

Could you post the updated code? When I run your code after fixing the j++ I get ["J","e","f","f"] as output.


#8

Here you go. Thank you again.

/*jshint multistr:true */
var text = "a;lsfj ;alsdjfowiENOVANPJeffl;sdfjl;aksdfj.";
var myName = "Jeff";
var hits = []
for(var i = 0; i < text.length; i++) {
if (text[i]==="J") 

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

#9

Ok why did you change the code so dramaticly? As said your original code with the fixed j++ already worked :smile: From what I can see your missing a { after the if line and the hits.push... should be inside of the second for loop it also helps if you indent the code inside loops and conditions that might make it more readable.


#10

I fixed it and now it's back to crashing:

/*jshint multistr:true */
var text = "alsdkfjl;askdjfl;a Jeff lsa;djla;skdfj.";
var myName = "Jeff";
var hits = []

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


#11

Well you're back using i++ instead of j++.


#12

Thank you. Now it says I have a "parse error":
/*jshint multistr:true */
var text = "a;lsfj ;alsdjfowiENOVANPJeffl;sdfjl;aksdfj.";
var myName = "Jeff";
var hits = []
for(var i = 0; i < text.length; i++) {
if (text[i]==="J")

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


#13

Either you missed an opening { here:

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

or you've an extra } here:

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

#14

I tried both options and both ended up with my browser crashing. Instead of a parse error it now says that I have a SyntaxError: expected expression, got '}'.


#15

Could you post your updated code?


#16

Here:
/*jshint multistr:true */
var text = "a;lsfj ;alsdjfowiENOVANPJeffl;sdfjl;aksdfj.";
var myName = "Jeff";
var hits = []
for(var i = 0; i < text.length; i++) {
if (text[i]==="J")

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


#17

Hello ytro419, you're discussion helped me correct my code so i'd like to help you back

/*jshint multistr:true */
var text = "a;lsfj ;alsdjfowiENOVANPJeffl;sdfjl;aksdfj.";
var myName = "Jeff";
var hits = []
for(var i = 0; i < text.length; i++) {
if (text[i]==="J")

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

that's you're code right? well I noticed on the second for loop you used i < (myName.length + i), please change the first i to j so it would become j < (myName.length + i)
hope i was helpful.


#18

Thank you dr_zed, I changed i < (myName.length + i) to j < (myName.length + i) but it still says that I have an unexpected token "}".


#19

Never mind, I fixed it. Thank you so much everyone.