Technical Error: Last practice for loop


#1

Moderators, forgive me if I've posted this in the wrong place but for the last hour, I haven't been able to use the 'Save & Submit' button. It just freezes my browser. I've logged in and out of the system, restarted my computer to no avail. Is there a bug is this part of the lesson?


#2

Seems like you may have made an infinite loop inside your code, what code was causing the issue?


#3

I'm actually getting it the same frozen behaviour for multiple lessons now @benjnev. The code for each lesson as below:


#4

The first one is an infinite loop.

Because you have i = 100; then you're saying i<=100; which it already is so it keeps looping.
I made this which works

for(var i = 100; i >=0; i= i-5){
  console.log(i);
}

And for the second one is also an infinite loops, but im not too confident on how to fix that one, I believe you also need to declare things like var i and var j

@mtf @albionsrefuge @haxor789 Any idea's for part 2?


#5

In the inner for loop: The counting variable is j but the variable that gets increased is i. So as j stays the same value the condition j < i+myName.length will always be true -> infinite loop.


#6

Ah, thanks @benjnev @haxor789.

So in that case, with my inner for loop, in order to correct is, my value needs to be j++ as opposed to i++. Am I right?


#7

Yes, I'll let haxor explain because all I can really say is that j is counting up till its equal with i then it pushes it to the hits array, then you console.log hits to get it to print the result


#8

Short: Yes:
The idea is that you search for the first letter if of your name. if you found it you enter the inner loop starting at the position i (where you found it). And add the next myName.length letters to your hits array. Obviously

j < (myName.length+i)

is the same as

j < i+myName.length

but in the second version it might be more obvious what you try to do here. So as j goes from j = i to j = i+myName.length it is j which needs to be increased. So yes.


#9

Cool, thanks @haxor789!