6. I get wrong output but codecademy would let me pass


#1



Hi,
could someone help me out?

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


I keep getting wrong input ( [ undefinded ] ), but can't find the mistake.
Thanks!


/*jshint multistr:true */
var text = "This is a text. It is Ruby very \
boring for now Ruby, but what are we Ruby \
to do. We need the exercise.";

var myName = "Ruby";

var hits =[];

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

if (hits.length === 0) {
    console.log("Your name wasn't found!") 
} else {
    console.log(hits)
};


#2

there are couple of problems in your second for loop:

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

you will make hits empty every time, instead of storing all the results, i recommend to remove hits = [] from inside your second for loop

you want to find the instances of your name in text, and if you find them push them to your hits array, so you want to push text[j], not myName[j]


#3

You're absolutely right, thank you!