Your second 'For' (infinite) loop


#1



Every time I run this code it crashes my browser which I'm guessing means it's an infinite loop.
I can't figure out where I've gone wrong though.


/*jshint multistr:true */

text = "My name is Jayse. I was born as Jayse. People call me Jayse. Say 'Hey Jayse!'";
var myName = "Jayse";
var hits = [];
for (i = 0; i < text.length; i++) {
    if (text[i] === 'J') {
        for (var j = i; j < (i + myName.length); i++) {
            hits.push(text[j]);
        }
    }
}


#2

@jaysem,

for ([initialization]; [condition]; [final-expression]) {
// statement
}

In your 2nd FOR-loop definition

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

you will have to change the [final-expression] from i++ into j++


#3

And you're a genius. Thank you very much for that!


#4

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