6. Log it!


#1

var text = "caleb, caleb, caleb, caleb, caleb, caleb";
var myName = "caleb";
var hits = [];

for(i=0; i<text.length; i++){
    if(text[i] === "c")
    for(j=0; j<i + myName.length; j++){
        hits.push(text[j]);
    }
}
if(hits.length = 0){
    console.log("Your name wasn't found!");
}
else
{
    console.log(hits);
}

I can tell this code has problems that shouldn't have even made it to the next lesson Log it!
I need help finding the errors in this code, Thanks.


#2

This code has two problems, first in the second for loop:

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

you don't want to set j to 0 (j=0), you want to set assign it the value of i:

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

i contains the index of your hit, then you use j to push the letters of you name into an array.

Second problem is in this line:

if(hits.length = 0){

you use a single equal sign here, which means assign. This condition will always be false, since 0 is considered false. You want to use 2 equal signs here to check if hits lenght is 0