6. Second loop should accept j = 0


#1

This code is valid too:

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

Right now it's being rejected with the reason "Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.", which is nonsense because it'll actually break the program.


#2

How would it break the program? The point is to take their approach so you see what is trying to be done.


#3

It will result in a JS equivalent of "index out of range" exception.


#4

And how would this result? Every instruction they provide, if properly followed, will result in good code.


#5

The point of the excercise it to append your name (length) from list indices.


#6

If you follow the instruction and replace "myName.length" with "i + myName.length", it'll result in a wrong program. Example.


#7

j should be equal to i. You must do what they tell you to, not just follow part of it.


#8

It doesn't even check if j is equal to i at the start of the loop. If I fix the mistake with i+myName.length, it'll accept my code, even though it's wrong.

The essence of this part of the lesson is that we should add a loop with as many iterations as there are characters in myName. It shouldn't require writing the exact same code, it should only require the same results.