Help with "hits" method


#1

Hey there, can someone explain to me how the "hits" method works, and where/how it fits into my code? I'm really stuck here... Thanks in advance. Below is my code so far.

/*jshint multistr:true */

var text = "Hello, meet James. James likes football. James doesn't like soccer.";
var myName = "James";
var hits = [];

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

#2

hits is not a method, it is an array. You're meant to push letters at the end of it

hits.push('B');
hits.push('o');
hits.push('b');
console.log(hits); #

B,o,b

Not quite like that though, rather you should loop through the text and search for the name.


#3

Oh okay, thanks a heap! I'm new to all this stuff.. :slightly_smiling:


#4

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);
}

This is the textbook example showing the proper syntax for the second |for| loop