Your second "for" loop (5/7)


#1

Hi,

Please help!!! I am starting to learn to code and get stuck in this part. Here is my code:

var text = "John blah blah blah John, blah John blah blah.";
var myName = "John";
var hits = [];

for (var i = 0; i < text.length; i++);
{if (text[i] === "J")

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

The error mentionned : It looks like your second 'for' loop isn't pushing values to the hits
array. Make sure it's working properly and that myName's text appears
somewhere in the text variable.


#2

@corerockstar52615 the problem is the semi-colon after your for statement:

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

the semi-colon signifies the end of a command, so the for loop never gets run in its entirety. take out that semi-colon and your code will work.


#3

Oh wow, thanks a lot. I've been searching for this problem for the entire days.

Thanks!!


#4

Pretty close. The truth is that conditions and loop only work on the next statement and most of the time we do the trick to wrap all our stuff in {} which count as exactly one statement (so this one statement is the next). Unfortunately here:

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

and here:

if (text[j] === myName[j-i]);

not the {} part comes next but the semicolon. And if you compare a semicolon to a period in a sentence than "." would probably be the shortest and most pointless sentence so just a semicolon is the shortest and most pointless statement. So being a statement end sign the semicolon does not inhibit the loop from being run it just runs it pointlessely e.g.

    for (var i = 0; i < text.length; i++);
set var to 0
check if i (0) is smaller than text.length 
execute next statement -> /*do nothing*/;
increase i by 1 (i++)
check if i (1) is smaller than text.length 
execute next statement -> /*do nothing*/;
increase i by 1 (i++)
...
check if i (text.length) is smaller than text.length 
it is not so stop here

So all this does is increasing i because it runs through the loop and when it is ready you executed the {} part as regular code.

And just for completion for if's it's really dangerous as it makes them completely pointless e.g. here:

 if (text[j] === myName[j-i]);

here the if decides whether the semicolon gets execued or not but the code in {} which should depend on this condition is executed nevertheless.


#5

Thanks for the explanation but i still don't understand this part of the code:

if (text[j] === myName[j-i]);

Can you explain more in details?


#6

@haxor789 thanks for the great explanation! that makes a lot of sense but it didn't even occur to me.


#7

About the semicolon after it:


Or do you mean this specific if statement?


#8

My code is:

var text = "blah blah blah blah Rachel blah blah Rachel blah blah."

var myName = 'Rachel'

var hits = []

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

But it keeps coming up with 'Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.'


#9

'Oops, try again. It looks like your second 'for' loop isn't pushing values to the hits array.

you're not pushing to hits but to newArray and you empty at the beginning of every loop. So instead of

 newArray = [];
 newArray.push(text[j]);
 newArray[0]

rather use just hits.push(text[j]);


#10

Thank you! It works now!