5 problem with second loop



I have a problem with lesson 5. Here's my code:

var text = "lkfadsjfdhdfljdsffdhj hjjdflfdsjjdfslk Noah lkdhfjlsj Noöhafldfjl Noah hdslföjf";
var myName = "Noah";
var hits = [];

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

I hope, you can help me.


You have created an infinite loop. Here:

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

myName.length + j means that it will add the value of j to the length. Since j keeps increasing if the length of myName > 0 then the loop will run forever.


biggest problem is in your first loop:

for(var i = 0; i > text.length; i++)

in the first run, i = 0, the text.length is lets say 70 so lets fill in the values:

for(var i = 0; 0 > 70; i++)

hmm.. 0 is not bigger then 70, it should be the other way around:

for(var i = 0; i < text.length; i++)

now i will increase, until it looped over all characters (then it will no longer be smaller then text.length)

Second for loop also has a problem, you want this:

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

why? now j will loop over the first hit (i) till the last (i + myName.length)



Now i understand it.


A post was split to a new topic: Second for loop