Logit it! Why it includes other words beside my name?


#1



Hello coders!

Can anyone tell my why my code isn't showing only my name(CATALIN) and also include the CARPENTER word?
My result look like this:
[ 'c', 'a', 'r', 'p', 'e', 'n', 't', 'c', 'a', 't', 'a', 'l', 'i', 'n' ]


https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/6?curriculum_id=506324b3a7dffd00020bf661


var text="weather carpenter my name is catalin";
var myName="catalin";
var hits=[];

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

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


#2

@catalinzaharia,
Because you are =testing= if the =character-found= equals "c"
and if so, you collect the next (myName.length) characters into the hits Array

If you would change the IF condition into

(text.substring(i,i+myName.length)===myName)

you would have a different hits =result=