Your second "for" loop


#1

https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/resume?curriculum_id=506324b3a7dffd00020bf661

/*jshint multistr:true */
var text= “Hey, how are you
doing? My name is Emily.”;
var myName= “Emily”;
var hits= [];
for (var i = 0; i === text.length; i++) {
if (text[i] === “E”) {
for(j=0; j < myName.length; j++){
hits.push(text[i+j]);

    }
}

}

For All!!! Where is a problem. I don’t uderstand this. I know this its easy but somebody help me. Many thanks:)


#2

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for
for ([initialization]; [condition]; [final-expression])

In your 1st FOR-loop you use
for (var i = 0; i === text.length; i++)
So you start by setting the variable i to the VALUE 0 (zero)
Then in the condition i === text.length you are thus testing if zero =equals= the length of your text…well since that is NOT the case, your FOR-loop will EXIT…!!
Change the condition into i < text.length

===============================
Maybe if you look at it in this way…

As programming is the attempt
to copy the reality…

You have a piece of text,
with your left-finger you scan this text
character for character, and keeping count with i

At every character you check
is this character the same as the firstcharacterof_myName
( thus myName[0] )
If so…
start reading with your right-finger at this position i
but for the right-finger we use counter j
so we start counting at j = i
we will [repeat] the following action
as long as we have incremented less than myName.length
thus as long as j < ( i + myName.length)
The action:
write the character of the right-finger in an Array
hits.push(text[j]);
goto [repeat]

so the second for-loop looks like
for (var j = i ; j < ( i + myName.length ) ; j++ ) {
hits.push( text[j] );
}


#3

ok a have next problem!! Thx


#4

You integrated the 2nd FOR-loop into the 1st FOR-loop…
Have a good look at your opening and closing curly-brackets-{ }


#5

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