Second for loop


Please help, why does my code find such a big quantity of my names if there are two names only in text.

var text = "Prosciutto venison strip steak leberkas.\
Meatloaf short ribs tenderloin t-bone. \
Meatloaf boudin shoulder, brisket beef rump \
ribeye Pavel picanha beef ribs jowl. \
Pavel chicken fatback venison pork belly. \
Pork chop bresaola beef ribs, pork loin jerky kielbasa short loin beef.";
var myName = "Pavel";
var hits = [];
for (var i=0; i < text.length; i++) {
if (text[i] === "P") {
for (var j=i; j < (myName.length + i); j++) {

if (hits.length === 0) {

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

Search Text - "Second 'For' Loop"



the second for loop loops over your name, so for each letter in your name, you append your name to hits. you should push text[j] into your hits array, since j contains the indexes of your name (thanks to the second for loop)


The guy above is correct. In the code quoted above you have a for loop that will loop multiple times and everytime it loops you are doing:


Which means that everytime you loop your are pushing your ENTIRE name to hits rather than each letter one at a time. What you want is:


so that you push each letter one at a time. Each time a new loop starts j will be increased by 1 and thus you will be pushing the next letter to hits.


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