Help understanding "Log it!"


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++) {

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

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


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 = [ ];




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


Thanks! That link you sent helps a lot


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: