Getting rid of the repeating letters of my name


#1


https://gist.github.com/1927b5301f92efdc762c61b423331589


No error and I'm done with the lesson but I'd like to know how to get rid of the repeating of my name in the log.



/*jshint multistr:true */
var text = "Hey, how are you \
doing? My name is Kyle. Kyle enjoys coding, but more importently, Kyle likes talking in third person.";
var myName = "Kyle";
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(text[j]);
        }
    }
}

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


#3

why would you want this? The design of the program is in such a way, that it find all instance of myName in text

If you only want your name once, you can simply remove the second for loop and instead push myName to hits array and then break the loop, then you have your name once

or you can break the loop first for loop after the second for loop has finished.


#4

Sorry I read the last part of the lesson wrong. It asks how can I have the console log ONLY my name, even if there is a word that starts with a K in var text?


#5

There are several ways you can do this. Another loop, to verify your name is an exact match. Its a good challenge, you should try it :slight_smile:

Think about how we can solve this problem, write psuedo code.

If you need help, please let me know

But i am not going to code this for you.


#6

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