What am I doing wrong? javascript "search text for your name"


#1

Instructions:
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.

/*jshint multistr:true */
var text = "Audi's 4.0 Twin-Turbo V8 is my Favorite Engine";
var myName = "Rohit";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[i] === myName[0]) {
for (var j = i; j < i + myName.length; j++) {
if(text[j] === myName[j-i]) {
hits.push(text[j]);
}
}
}
}

If i change the === to only one = in line 6 it accepts the code.


#2

Hi here

if (text[i] === myName[0])

You should do it

if (text[i] ===  "R")

And here

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

In this part you dont need of the if statement just remove it. So it should be like that

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

    hits.push(text[j]);

}

#3

so this is what i have and it gives an error saying "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"

/*jshint multistr:true */
var text = "Audi's 4.0 Twin-Turbo V8 is my Favorite Engine";
var myName = "Rohit";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[i] === 'R') {
for (var j = i; j < i + myName.length; j++) {
hits.push(text[j]);
}
}
}


#4

Inside your var text you should put at least one time your name.

var text = "Audi's 4.0 Twin-Turbo V8 is my Favorite Engine";