6. Log It! - Array full of Undefined Items


#1

Hello,

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++){
            hits.push(myName[j]);
            }
        }
    };
    
if (hits === []){
    console.log("Your name wasn't found!");
    }
else {
    console.log(hits);
    }

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

Thanks for any help!


#2

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


#4

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


#5

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