Your second 'for' loop


I get the following error, and I've sifted through a lot of other topics for help already.

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.

My code is as follows.

var text = "me g me gg g";
var myName = "me";
var hits = [];

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

However, when I change the

if (text[i] === "me") {


if (text[i] === "g") {

I get a successful working code. Not sure why. Plus, anything more than 2 letters in that 'IF' condition, will generate that error of not pushing values to the hit array.

Console log: (note that 5 'g' are printed instead of 4.

[ 'g', ' ', 'g', 'g', 'g', ' ', 'g', undefined ]

Then another test I did was to change it to

if (text[i] === "m") {

And I got the following result from console log:

[ 'm', 'e', 'm', 'e' ]

Similar results appear even if I changed the 'IF' to "myName" instead of repeating the string (where var myName = "g")

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

[ 'g', 'g', 'g', 'g' ]

Would really love an explanation

Edit: I've solved the problem on my own after an hour of fiddling around with the code, but still unable to understand why it can't take in a whole name, but needs to check the first name, which causes words that are starting after that single letter to be printed.


the first for loop looks for the first letter of your name, then the second for loop is used to loop over your name once the first letter has been found, and pushes it inside an array

You could of course design the program differently, but this is the design chosen in the exercise


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