5. Your second "for" loop



I am not sure what to do here:

Your second "for" loop
Okay! Last loopy step: add another for loop, this time inside the body of your if statement (between the if's {}s).

This loop will make sure each character of your name gets pushed to the final array. The if statement says: "If we find the first letter of the name, start the second for loop!" This loop says: "I'm going to add characters to the array until I hit the length of the user's name." So if your name is 11 letters long, your loop should add 11 characters to hits if it ever sees the first letter of myName in text.

For your second for loop, keep the following in mind:

First, you'll want to set your second loop's iterator to start at the first one, so it picks up where that one left off. If your first loop starts with

for(var i = 0; // rest of loop setup
your second should be something like

for(var j = i; // rest of loop setup
Second, think hard about when your loop should stop. Check the Hint if you get stuck!

Finally, in the body of your loop, have your program use the .push() method of hits. Just like strings and arrays have a .length method, arrays have a .push() method that adds the thing between parentheses to the end of the array. For example,

newArray = [];
newArray[0]; // equals 'hello'
Okay! Go ahead and add that second for loop inside the body of your if statement.




Could you please show the code that you have written for this exercise, or until this exercise?


This is my code in 4. Your "if" statement:

var text = "Qasim";
var myName = "Qasim";
var hits = [];

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


Now that you have found out that your name in the string text begins at index i. You can create another loop.

The loop would begin at i and extract myName.length characters from text. Each character would be pushed in hits array. Do you know how to do this? :wink:


How do we do that? i dont know


A loop has this syntax:

for(variable initialization; test condition; update variable){

Use a variable j that begins at i. Make it run myName.length times. Push each character at index j to hits array.

If you are not able to understand, please think about it repeatedly for some time.