Passed but not with intended results


#1

this is my code when ran i get ['','',undefined]

var text = "hey my name is Lake black. Lake is a great name. Lake";
var myName = "Lake";
var hits = [];

for(var i = 0; i < text.length; i++) {
if(text[i] === myName[0]) {
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);
}


#2

Semicolon after the loop terminates loop and code in the block of the loop is executed only once. This is why your code:

for(var j = i; j < (myName.length + i); j++); // <-- unwanted semicolon, should be replaced with {
  hits.push(text[j]);
}

works exactly the same as this code:

hits.push(text[j]);

Loop is terminated and that is why text[j] is undefined. You have to delete the semicolon.