5/9 Error with syntax


#1

var text = "I am currently trying to learn javascript";
var myName = "Hayden";
var hits = [];

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

this is the code I used, honestely, I don't fully understand what I have to or am doing.

can someone explain in more details and why my code is having problem?


#2

If you look at the end of your for loop, you have a semi-colon just chilling there.(It shouldn't be.:stuck_out_tongue:)
Also, try to indent the code, and separate the lines and curly brackets more so it's easier to spot mistakes like this.

var text = "I am currently trying to learn javascript";
var myName = "Hayden";
var hits = [];

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

#3

Hi, Hayden
The thing here is, that the var text should containt at least 2-3 times your name, in order for your code to find it. Since your var text does not contain the word Hayden, although your code might be correct, there is nothing to display... no Hayden, no output in console. :smile:

var text = ("Hello everyone, my name is Alan. Alan is a mens name, so I am a man, not a woman named Alan. Alan is a very common name. Everybody should be called Alan.");
var myName = ("Alan");
var hits = [];
for ( var i = 0; i < text.length ; i++) {
    if (text[i] === 'A') {
        for (var j = i; j < (i+myName.length); j++) {
           hits.push(text[j]); 
        }        
    }
}