Second "for" loop halp plz


#1

var text = "once upona time autocorrect never existed jose was vrry happu in this land. ot waz a land with jose typinh hid name poinmcorrectlu.";
var myName = "jose";
var hits = [];
for(var i = 0; i < text.length; i+= 1) {
if (text[i] === 'E'){

}}

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!

What do i do?


#2
var text = "once upona time autocorrect never existed jose was vrry"+
           " happu in this land. ot waz a land with jose typinh hid"+
           " name poinmcorrectlu.";
var myName = "jose";
var hits = [];
for(var i = 0; i < text.length; i+= 1) {
   if (text[i] === 'j'){
        for (var j = i; j < myName.length + i ; j +=1) {
            hits.push[ text[j] ];
        }
   }
}
console.log( hits);

#3

it didnt work oh noo. it said"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." the console just said "[]". >:frowning:


#4

thx it worked but the console displayed nothing. i have
var text = "once upona time autocorrect never existed jose was vrry happu in this land. ot waz a land with jose typinh hid name poinmcorrectlu.";
var myName = "jose";
var hits = [];
for(var i = 0; i < text.length; i+= 1) {
if (text[i] === 'j'){
for (var j = i; j < myName.length + i ; j +=1) {
hits.push( text[j] );
hits[j];
}
}
}


#5

you need

console.log(hits)

at the end.


#6

Also it's probably best to get rid of that lonely "hits[j];" that's just sitting at the bottom of your for loop. It looks so sad D: Go ahead and wipe it from existence so it can be with its friends!


#7

agreed, new to this as well. Missed that little guy


#8

var text = "Blah blah blah blah blah blah Sara \
var text ="blah blah blah Sara blah blah Sara blah blah \
blah blah blah blah blah Sara";
var myName = "Sara";
var hits = [];
for(var i = 0; i < text.length; i++){
if(text[i] ==="S");
for(var j = i; j<(myName.length + i); j++){
hits.push(text[j]);
}
}