JS: Search Text for Your Name: Your second "for" loop


Hello World,
here is my code and the error I am getting. Thank you for any help!

/*jshint multistr:true */
var text = "Jingle all the way Muddertruckers!"
var myName = "Mike"
var hits = []
for ( i = 0; i < text.length; i++) {
if (text[i] === "M") {
for (var j = i; j < (i + "Mike".length); j++){



Oops, try again. Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.

Output is this: [ 'M' ]
[ 'M', 'u' ]
[ 'M', 'u', 'd' ]
[ 'M', 'u', 'd', 'd' ]


your second for loop:

for (var j = i; j < (i + "Mike".length); j++){

it seems the exercise wants you to use i + myName.length, to not hardcode your name, again


Bam, thank you so much! I never would have got that. I will pay it forward if I survive to become a guru, haha. Thanks again stetim94!


what you do is technically not wrong. The exercise doesn't approve it, which i think is a good thing.

Lets say you know want to change the name:

var myName = "Thomas"

then you would have to update here:

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

and here:

for (var j = i; j < (i + "Thomas".length); j++)

if you would do this:

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

you can just update myName super easy, you only have to update myName, that are all the changes you need to make


Ahhh, k very cool. The D.R.Y. method prevails! I love the community aspect of this! Codecademy rules! Thank you.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.