5 Your 2nd for loop


#1

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

Error :

Oops, try again.
Careful: Your second 'for' loop should stop when it reaches its current point in the string + myName.length

#2

Little hint: if j = i, then j - i is going to be 0.


#3

Playing with my completed code:

j <= myName.length + i

works, but

(j - i) <= myName.length

(Which, logically should be identical) does not work.

I'm not sure if this issue comes from the Codecademy checker, or if indexes in nested for loops need a particular setup in JavaScript.


#4

I think there might be a deeper issue at play here.


#5

How is that identical? if i = 5 and j = i, then j - i = 0 (which will always be less than the length of your name), but if I = 5 and your name has 5 letters, then while j is less than 10 it will run.


#6

If i = 5 and your name had 5 letters both equations would be true for j < 10.
It's j <= 5 + 5 versus j - 5 <= 5.

Here's my proof that logically (not syntactically) it shouldn't matter which side of the inequality you put i on.

Suppose your name length is 2

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

j begins at i. Therefore j < myName.length + i is True. The loop runs for the first time; j++.
Now j = i + 1. Therefore j < myName.length + i is True. The loop runs for the second time; j++.
Now j = i + 2. Therefore j < myName.length + i is False. The loop ends after running twice.

Case 2: (var j = i; (j - i) < myName.length; j++)

j begins at i. Therefore (j - i) < myName.length is True. The loop runs for the first time; j++.
Now j = i + 1. Therefore (j - i) < myName.length is True. The loop runs for the second time; j++.
Now j = i + 2. Therefore (j - i) < myName.length is False. The loop ends after running twice.

The loop would run the same number of times for both cases regardless of i or name length.

.
.
.
Edit: What You'll Be Building accepts all of these, and all of the outputs are the same. It seems like the code checker on parts 5-7 is just picky.
for(var j = i; j < (myName.length) + i; j++) - default
for(var j = i; (j-i) < (myName.length); j++)
for(var j = i; 0 < (myName.length) + i - j; j++)


#7

Heres my rundown of how it works.


#8

i + (j < MyName.length) wouldn't work because it tries to add a value/number (i) to a Boolean (j < MyName.length), which will not (edit: usually) result in a Boolean.

(j-i) < myName.length does result in a Boolean, and would normally work. (In fact, it does work in the earlier exercise What You'll Be Building). It only fails here because the code checker is specifically looking for myName.length + i.


#9

I know, i was responding to the OP, you can see that. And it would work, because it would result in true which is equal to the int 1. However that was the obsurdity I had to figure out before I understood how the script worked.

Oh I didnt know about the excersise differences.


#10

i + (j < MyName.length) would result in 1+i which will never be False. Therefore the loop has no end condition and will result in an infinite loop. (Unless you're lucky enough to be at i = -1. Then the loop is false and never runs.)

(j-i) < myName.length does not have this problem.


#11

You seem to think i wrote that code, i know, i meant it would work because you can add a bool; not that it was right.


#12

I know you didn't write it, I'm talking about it because it has a different issue than OP's code and is not applicable to OP's situation.

Edit: The solution you provided to that OP is the same code this OP is getting an error on. The end point of your link is the starting point of this issue. It's not a problem understanding the code - it's a problem with the checker.


#13

OP,

(j-i) < myName.length should work just like j < myName.length + 1, or 100 < myName.length +1 - j +100. If you don't believe me, experiment with the for loop conditions on the example at the beginning of the lesson.

I think it's not working for you here because the code checker is looking for myName.length +1 specifically. I think this because the instructions and hint heavily imply that this is the equation they want, and because the checker has done similar things in the past. (On one project I got random errors for using the word "briefcase" in my text because the checker was counting the number of times "case" appeared in the entire code.)

I'm going to be afk all weekend and wanted to give a quick tl:dr summary before heading out. I'll be happy to answer any remaining questions on Monday.

borgette out!


#14

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