Frustration with 2nd loop: Oops, try again. Make sure you're pushing letters to the hits array!


#1

I am not sure why I am getting the error message "Oops, try again. Make sure you're pushing letters to the hits array!". I have looked at the other posts on this, but was not able to locate where my error is. Any help would be greatly appreciated.

var text = "A string containing some text made by Rick \
            Rick, Rick, Rick";
var myName = "Rick";
var hits =[]

for (i = 0; i < text.length; i ++){
    
    if (text[i] === "R"){
        hits[i] = text[i];
        
        for (j = i; j < (i + myName.length);j++){
            hits.push(i);
        }
      }
    }

#2

This line here is not needed...

hits[i] = text[i];

Your push() handles assigning the character to the array.

Also it's not hits.push(i)
It's hits.push(text(j);

for(var i = 0; i < text.length; i++){

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

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


#3

correction, just in case...it's hits.push(text[j]);

My apologies for mistyping it.


#4

Thank you. That worked.


#5

Hi! I'm having a similar issue, keep getting an error message that says "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."

PLEASE, could you help me locate where my error may be...

var text = "Hi my name is Mel";
var myName = "Mel";
var hits = [];
for (var i = 0; i < text.length; i = i + 1)
console.log(i);{
if(text[i] === 'M') {
for(var j = i; j < (i + myName.length); j++) {
hits.push(text[j]);
}
}
}


#6

Hi. I would try to remove the console.log(i) form the first for loop. Also the curly bracket should be right after the conditions for the loop. You also do not need to use the var keyword in the second loop. Please try this

var text = "Hi my name is Mel";
var myName - "Mel";
var hits = [];
for (var i = 0; i < text.length; i++) {
if (text[i] === "M") {
for(j = 1; j < ( i + myName.length); j++){
hits.push(text[j]);
}
}
}

Hope that helps


#7

hey my code isnt working either please help
/*jshint multistr:true */
var text = "harrison is an utter beast like a cow, harrison can eat 10 mod pizzas in 20 seconds"
var myName = "harrison"
var hits =[]
for(var i = 0; i < text.length; i++)
{
if (text[i] === "h")
{
for(var h = i; h < i + myName.length; h++)
{
if (text[i]
{
hits.push(text[h]);
};
};
};
};