Search Text for your name? Need help


#1

var text = "blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
var myName = "Eric";
var hits = [ ];

// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
if (text[i] === "E") {
// If we find it, add characters up to
// the length of my name to the array
for(var j = i; j hits.push(text[j]);
}
}
}

if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}

where did i go wrong?

appreciate your help


#2

Hi @vvang044

I corrected your code, I hope you'll understand your mistakes :

var text = "blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \ ";
var myName = "Eric";
var hits = [];

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

if(hits.length === 0){
    console.log("Your name wasn't found!");
}else{
    console.log(hits);
};

Hold on :wink:


#3

Awesome thank you..........................