6. Log It! - Array full of Undefined Items



I when I submit my code, it passes it, saying that it works. But rather than getting an array with my name in it, I get several undefined items in my hits array.

Here is my code:

var hits = [],
    myName = 'Keisha',
    text = 'My name is Keisha. I am 28 years old and I love \
pizza. Do you love pizza like Keisha?';

for (var i = 0; i < text.length; i++){
    if ( text[i] === myName[0]){
        for (var j = i; j < (i + myName.length); j++){
if (hits === []){
    console.log("Your name wasn't found!");
else {

I'm sure there's something small I'm missing. But I'd like to get this right.

Thanks for any help!


If j is myName.length or higher it is out of bounds, and unreadable, so undefined. Use text[j] instead.


Thanks so much! I couldn't figure it out.


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