Your second "for" loop - infinite loop/freeze/error?


#1

OK so I'm not only having a hard time understanding the concept and execution of this lesson but I think i have it right and the web app freezes up when I click 'Save & Submit Code'.

Nothing happens and my browser freezes. Am I stuck in an infinite loop? If so, how?

Here is what I have:

/*jshint multistr:true */

var text = "Hello! I am an aspiring web developer.  My Name is Ryan. \ I heard from someone that someone wanted me to be a junior to my father but I'm glad they stuck with Ryan."

var myName = "Ryan"

var hits = []

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

I have cleared cache in browser, restarted computer/browser, all of the 'tech support' stuff and nothing seems to help.

Have I broken Codecademy's text editor somehow :stuck_out_tongue: ?

But more importantly, is my code right?

Thank you for any and all feedback.


#2

it freezes because this for loop is infinite. You wrote i++ instead of j++
so then it stands to reason that j will always be less than i + myName.length


#3

I literally was just editing this post to ask if it could be an infinite loop. haha. Thank you mucho for this reply, @rydan


#4

No probs Happy coding buddy :smile:


#5

OK, so I've updated my code and went back over it a few times and I feel like I understand half of what happened.

I've been told that I shouldn't get hung up on something for too terribly long and to move forward anyways. The main reasoning from the people who have told me this is that I'm going to learn as I go and it'll all start to fall into place on it's own.

In your opinion, @rydan, is this good practice? or should I stay on it, beat it onto my brain and only then move on?


#6

@rkellum
if you stay on and you are learning some other useful elements in the process that it wouldn't hurt to keep hitting the net for the problem.
But if you are not learning anything new in the process of thinking and thinking the problem out and it bearing no fruit then its best to seek help

Sometimes when I am solving problems and I am stuck I try to research the problem for sometime and in that process I learn new way and process till i can solve it
but if the problem is still persisting then best you seek help.

try Codewars for challenges too


#7

I was on this same problem and I could never figure out why it was happening but I knew it was an infinity loop. Thanks for solving this problem @rydan.


#8

Is it possible to check where I went wrong? I am also unable to move forward and need help.

var text = "I Letitia love eating chocolate. My name is Letitia.";

var myName = "Letitia";
var hits = [];
for (var i = 0; i < text.length; i++){
console.log(text[i]);

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