Is it a good solution?


#1

I'd like to know if the following code is a good solution to the exercise, or if there are some critical points that I can't see. Of course, apparently it works fine :smile:

CODE:

var text = "When I was a child, everyone called me \ Pier. Since then, long time has passed but people still continue to call me Pier.";

var myName = "Pier";

var hits = [];

for ( i = 0; i < text.length; i++) {

if (text[i] === "P" && text[i+(myName.length-1)] === "r") {

    for (j=i; j<(myName.length + i); j++) {
    
        hits.push(text[j]);
    
    }

}

}

if (hits.length === 0) {

console.log("Your name wasn't found!");

} else {

console.log(hits);

}