Pushing letters to the array... help!


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661#

Currently getting "Oops, try again. Make sure you're pushing letters to the hits array!"


var text = "Hi my name is Carrie. What's your name?";
var myName = "Carrie";
var hits = [0];

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


#2

Hello :slight_smile:

There are two problems in your code.

First one, your hits array should be initially empty -> var hits = [];.

Second one, semicolon after for loop:

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

terminates it, so code inside this for is executed only once. You have to delete the semicolon.


#3

Thank you so much!!! :sunny:


#4

You're very welcome :panda_face:


#5

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