Your "for" second loop


#1

var text = "My name is Francisco\
Blah blah Francisco"
var myName = "Francisco"
var hits = []

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

I am totally lost here. I'm pretty sure the problem is in the hits.push line (and maybe the one above), but I don't understand the instructions. Can someone please check my code and give me some advice on how to move on? I've been stuck here for quite some time...

Thanks in advance!


#2

you have a problem here. it should be

for (var j=i; j<i+myName.length; j++)

Now i will leave it up to you to figure out what to push to the array


#3

Thanks! It doesn't totally solve it, but it's a start :wink:


#4

you have the same problem here


#5

here your code logic will not work . if the first letter appearing in your text is F then and only then the second for loop will begin but that is not the case with the text you have given. and that is not the expectation of the task. it should rather be

if (text[i] === "F"){

but we can make more dynamic by writing

if (text[i] === myName[0]){

#6

Got it, makes total sense. Thanks!
I also figured out the hits.push part:
hits.push(myName)
Honestly it was a lucky guess because I still don't get what this method does...


#7

Awesome but try to work a little bit more on your code its not totally correct.
for starters use this as your text

var text = "My name is Francisco\
Blah Fblah Francisco. Fans are my Favourite people";

Now after the entire code do this

console.log(hits);
console.log(hits.length);//we expect it to be 18

#8

Ok so now it looks like this (never mind the *, I had to write it here because if I just used < some parts of the code wouldn't show)

var text = "My name is Francisco\
Blah Fblah Francisco. Fans are my Favourite people"
var myName = "Francisco"
var hits = []

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

console.log(hits);
console.log(hits.length);

the hits.length prints as 45 for some reason...


#9

Yes check if the hits hits array only contains the characters of your name


#10

Ok so I think I finally get the logic of the code, but now I don't see why the console.log(hits) prints out 45 and not 18.

How do I check if the hits array only contains the characters of my name? I think it does but obviously something doesn't add up!


#11

when you run the code with the print statement i told you to add you will get

[ 'F',
  'r',
  'a',
  'n',
  'c',
  'i',
  's',
  'c',
  'o',
  'F',
  'b',
  'l',
  'a',
  'h',
  ' ',
  'F',
  'r',
  'a',
  'F',
  'r',
  'a',
  'n',
  'c',
  'i',
  's',
  'c',
  'o',
  'F',
  'a',
  'n',
  's',
  ' ',
  'a',
  'r',
  'e',
  ' ',
  'F',
  'a',
  'v',
  'o',
  'u',
  'r',
  'i',
  't',
  'e' ]

looks like there are some unwanted characters in there


#12

Take a look at this post to get an idea what to do
Click here to post


#13

Thanks, I'm resetting the whole thing and starting all over again.
I will use this post as reference!


#14

Ok so I went through the whole thing again, here is my code now:

var text = "My name is Francisco, I am from France.\
My father is also named Francisco."
var myName = "Francisco"
var hits = []

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

It's working, however when I print it, it prints not only my name, but everything that starts with "F" and keep printing for the length of my name.

It's not perfect, but I went throught the exercise until the end and in the last page they say that it is normal. To stop this I would need more advanced knowledge or some sort of plugin.

So I guess I've done it! Thank you so much for all the help!!