5 of Search Text For Your Name | Your second "for" loop


#1

var text = "My problem is Yanyan. I hate Yanyan. There's something wrong with Yanyan.";
var myName = "Yanyan";
hits = [];

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

Anything wrong with this code? It keeps on prompting: Oops, try again. Make sure you're pushing the letters to the hits array.


#2

@dyan_aguirre03gmail_,

In Javascript a semi-colon-; is an End-of-Statement indicator,
causing the-rest-of-statement not being executed....
Change your FOR-loop's
from

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

into

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

#3

var text = "My problem is Yanyan. I hate Yanyan. There's something wrong with Yanyan.";
var myName = "Yanyan";
hits = [];

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


#4

@13jepytlar,

var text = "My problem is Yanyan. I hate Yanyan. There's something wrong with Yanyan.";
var myName = "Yanyan";
hits = [];

for (var i = 0; i < text.length; i++) {
    console.log(text[i]);

    if (text[i] === 'Y'){

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

console.log( hits );

#5

Hi All!

Thank you so much! I got it now. You guys rock. (:smile: