Log It! Problem - Name prints to console 12x Instead of 3x


#1


The problem is telling me that I've written code correctly to search for the hits on my name, but instead of the number of hits, I'm seeing my name print out, and it's printing out 12 times, when I have my name listed in the text variable only three times. Not sure where I'm going wrong here.



/*jshint multistr:true */

var text = "My name is Kim, but people mispronounce Kim all the time.  They think I'm Tim or Ken, but I'm really Kim.";
var myName = "Kim";
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('Kim')
        }
    }
}

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


#2

this line should refer to your text variable and the for loop variable j,

to,

hits.push(text[j])


#3

When I implement the change tot the hits.push line of coding as you suggested, I get the following error:

When i switch 'Kim' out for the variable 'i', I get: [ 'K', 'K', 'K', 'K', 'K', 'K' ] in the log.

When I switch out 'Kim' and 'i' for the variable 'j', I get [ 'i', 'm', 'i', 'm', 'i', 'm' ], but no 'k' showing up.


#4

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