Hi I am currently doing a javascript course at Code Academy pertaining to "for" loops.
but this exercise does not make sense to me and it is pointless going further without understanding why I am doing what i am doing... here with the code.
text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";
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 < (myName.length + i); j++) {
hits.push(text[j]);
}
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
I get the first loop but what confuses me is the whole second loop inside the if statement....
for(var j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
}
just dont understand what i am doing?
text[i..i+myName.length-1]onto thehitsarray.) The conditional if-statement should likely be inside the inner loop, with a slight modification also takingmyName[..]into account. Thehitsarray should only be added to when a "complete" name is found.