Your second for loop isn't pushin values into hits array


#1

/*jshint multistr:true */

var text = "This is a string will containing will some will texts";

var myName = "Will";

var hits = [];

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

I couldn't pass this challenge with the above code. Please help correct the codes. Thanks.


Your second 'for' loop isn't pushing values to the hits array
#2

the code relies on the fact, that Will is spelled with first uppercase letter in your text variable.


#3

Thanks for your quick reply.
By changing if(text[i] === "W") from W (uppercase) to w (lowercase), I passed the challenge.


#4

But, now every word that starts with a w gets pushed in your hits array. The idea was you did the exact opposite, change will in your text variable:

var text = "This is a string Will containing Will some Will texts";

and then change your if condition back to:

if(text[i] === "W")

#5

I got it. Thank you very much.