Log it! Returns my name and undefined?


#1

/*jshint multistr:true */
var text = "Jose is learning to write some JS by any means possible and I \ don't care how I get there just as long as I get there";

var myName = "Jose Reyna";

var hits = [];

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

I passed the quiz but I am also receiving undefined. Did I not define a variable?


#2

here:

for (let j = i; j < [i] + myName.length; j++)

why is i inside square brackets?

That should fix a big part of your code.

Next, this line:

hits.push(myName[j]);

why push values from myName? The purpose of this program is to find your name in a string (the string stored in text), so it makes sense to push letters from text into hits

Finally, if the name you search for is Jose Reyna, that is also what should be present in the string stored in text. Currently there is no surname after Jose.

Also, the J of JS will also be seen as your name (this is only a basic program)