I do not know what is wrong and why we have to put hits.push(text[n]); instead of just hits.push(n);


#1

var text = "My name is Shawn in here";
var myName = "Shawn";
var hits = [];
//loops by an increment of 1 to scan text length
for (var i = 0; i < text.length; i++) {
//for each increment of 1 in text
//if first array value === "S"
if (myName[0] === text["S"]) {
//if first array value === "S"
//for n === myName.length + i
for (var n = i; n < (myName.length + i); n++) {
//push to array hits
hits.push(text[n]);
}
}
}


#2

@a.john,

the 1st FOR loop

In your 1st FOR-loop you are using the variable i
which has a number VALUE in the range of 0 till the length of the text.

With text[0] you have grabbed the 1st Character of the text
thus
if you use text[i] you will iterate over the text character by character

the comparison between text[I] and myName[0]

With every iteration over the text using the number VALUE i
you will want to compare the grabbed text character text[i]
with the 1st Character of the associated string VALUE of the myName variable being myName[0]
thus
change your if (myName[0] === text["S"]) {
into if (myName[0] === text[i]) {

the 2nd FOR loop

In the 2nd FOR-loop
you use an initialization of var n = i ;
therewith giving the variable n the current number VALUE of i
In this 2nd FOR loop you want to push the found string VALUE character of text[n]
into the hits Array
and NOT as you questioned the number VALUE of n

suggestion

Add a
console.log( hits );
as ending og your code, to see the result of your coding.....