Search Text - "Second 'For' Loop"


I don't understand what to do at all. Will somebody please help me???


Err... Okay!

Drop link of exercise and/or instructions and I/We could help explain what you should do.



So, you initially have something like this:

/*jshint multistr:true */

var text = "My name is xxxx and I am a boy.";
var myName = "xxxx";
var hits = [];
for (var i=0; i < text.length; i++); 
if (text[i] === "S");

What's needed, is to add another for loop, inside the body of your if statement. However, this loop will make sure each character of your name gets pushed to the an array, called hits.

Now, you will need to remove the last semi-colon, first.

Your for loop this time around, unlike the first, should be:

for (var j=i; j< myName.length+i; j++){

Next, if your name is 4 letters long, your loop should add 4 characters to hits if it ever sees the first letter of myName in text. You will need to use the push method to make sure each character of your name gets pushed to the hits array.

Inside the curly brace of the for loop, do:


j is a variable which we already defined in our second for loop.

You might wish to see all that happened by doing:



You may also wish to checkout this thread: 5. Your second "for" loop.

I hope this helps! :wink:


Thank you so much!!! I finally understand it. Thank you!!! :smile: :smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile::smile:


You're welcome.

Have fun coding! :slight_smile:


2 posts were split to a new topic: Second for loop


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