Second for loopThe result of my code is a number


#1


Hi all,
I am working on the second for loop exercice, the goal is to find my name in the text.

My code is accepted but the result is a number = 29. I am assuming it is counting the letters of my name 9 * 3 = 27 and possibly adding a space in the 3 situations.
Below is my code:


`
/*jshint multistr:true */

var text = "O meu nome é Francisca. A minha mãe sempre gostou do nome Francisca por isso é que me chamo Francisca.";
var myName = "Francisca";
var hits =[];

for (var i=0; i < text.length; i++)
{
if ( text[i] === "F")
{
for (var j=i; j<(myName.length + i);j++)
{
hits.push(text[j]);
}
}
}
if (hits.push(text[j]) === 0)
{ console.log("Your name wasn't found")}
else
{ console.log(hits.push(j))}


#2

29 is the number of items in hits

if you are still at the second for loop, why this code:

if (hits.push(text[j]) === 0)
{ console.log("Your name wasn't found")}
else
{ console.log(hits.push(j))}

it looks a bit like the code for the next exercise, with a few differences (you don't check the length of hits equals zero) and you don't log hits to the console, but rather hits.push(j), i am unsure why


#3

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