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


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' ]


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

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


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


you would have a different hits =result=