Help understanding "Log it!"


#1

So this is the code that I got after several tries.

var text = "Hey! How's it going, Kenny?";

var myName = "Kenny";

var hits = [];

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

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

It worked great but I just need help understanding this part at the end:

console.log(hits[0]);

Before I got it right I had it as console.log(hits) and the name printed 5 times in the console. Why would it do that if there were no values in the variable that we set earlier with var hits = [ ];


#2

here:

hits.push('Kenny');

your second for loop should have pushed each letter in to the array, not your whole name

because .push() is designed to push elements into an array? from mdn - push:

The push() method adds one or more elements to the end of an array


#3

Thanks! That link you sent helps a lot


#4

if you ever need to understand something about js you can simply search in google: mdn push

or whatever it is you need to find, in this case it was push

good i could help :slight_smile:


#5